2016-11-19 1 views
0

ViewController.swift에서 예약 된 알림을 만들고이 알림에 두 가지 작업을 추가해야합니다. '통화'라고 말한 부분과 '취소'라고 말한 부분 ViewController.swift에 이러한 작업을 추가하려면 어떻게합니까? 여기 내 ViewController.swift 내 알림을 발사하는 기능이 코드의 일부입니다감가 상각 된 UILocalNotification을 대체하고 iOS 10에서 UNNotificationAction을 추가하는 방법

func notificationFires(){ 

    let notification = UILocalNotification() 
    // 2 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.fireDate = datePicker.date 

    // 3 
    if textField.text == "" { 

     notification.alertBody = "You have a call right now!" 

    } 
    else{ 

     notification.alertBody = self.textField.text 

    } 
    // 4 
    notification.timeZone = NSTimeZone.default 
    // 5 
    // 6 
    notification.applicationIconBadgeNumber = 1 
    // 7 
    UIApplication.shared.scheduleLocalNotification(notification) 



    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
     print("Recived: notification") 



     if cancelled == true{ 
      print("cancelled happened") 

     } 
     func cancelNotify(){ 
      cancelled = true 
      UIApplication.shared.cancelAllLocalNotifications() 
     } 
     completionHandler(.newData) 

    } 

} 
+0

어쩌면 당신이 찾고있는 것 : http://www.appcoda.com/local-notifications-ios8/ – Frankie

+0

+ Frankie 나는 그 웹 사이트를 시험해 보았고 많은 오류가 발생했습니다. – krish

+0

질문이 의미가 없습니다. 로컬 알림을 예약하고 있는데 왜 remoteNotifications를 확인하고 있습니까? – Adeel

답변

2

나는 아직 아이폰 OS 10에서 알림을 사용하지 않은, 그래서 내가 나서서 학습으로 파악 나 자신을위한 경험, 이제 나는 당신에게 그것을 전달할 수 있습니다.

UILocalNotification은 iOS 10에서 감가 상각되며 UserNotifications 프레임 워크로 대체되었습니다. 알림을 표시 할 수있는 사용자로부터 AppDelegate GET 인증에서

UNUserNotificationCenterDelegate으로 센터 대리자를 설정 :

현재
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 

    let center = UNUserNotificationCenter.current() 
    center.delegate = self 
    let options: UNAuthorizationOptions = [.alert, .sound]; 
    center.requestAuthorization(options: options) { 
     (granted, error) in 
     if !granted { 
      print("Something went wrong") 
     } 
    } 

    return true 
} 

사용자에게 알림 :

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    // Play sound and show alert to the user 
    completionHandler([.alert,.sound]) 
} 

작업을 처리 :

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    // Determine the user action 
    switch response.actionIdentifier { 
    case UNNotificationDismissActionIdentifier: 
     print("Dismiss Action") 
    case UNNotificationDefaultActionIdentifier: 
     print("Default") 
    case "foo": 
     print("foo") 
    case "bar": 
     print("bar") 
    default: 
     print("Unknown action") 
    } 
    completionHandler() 
} 

원하는대로 수행하십시오. 앱의 모든 알림에 대한 모든 작업 및 카테고리를 설정합니다.

func setupActions() { 

    //create first action 
    let foo = UNNotificationAction(
     identifier: "foo", 
     title: "foo" 
    ) 

    //create second action 
    let bar = UNNotificationAction(
     identifier: "bar", 
     title: "bar", 
     options: [.destructive] 
    ) 

    //put the two actions into a category and give it an identifier 
    let cat = UNNotificationCategory(
     identifier: "cat", 
     actions: [foo, bar], 
     intentIdentifiers: [] 
    ) 

    //add the category to the notification center 
    UNUserNotificationCenter.current().setNotificationCategories([cat]) 
} 

그리고 마지막으로 실제 통지를 만드는 : 그들이 중심이 아닌 통지 자체에 할당하고 있기 때문에

func setupNotification() { 

    let content = UNMutableNotificationContent() 

    content.title = "Hello!" 
    content.body = "A message" 
    content.sound = UNNotificationSound.default() 

    //make sure to assign the correct category identifier 
    content.categoryIdentifier = "cat" 

    // Deliver the notification in five seconds. 
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 
    let request = UNNotificationRequest(identifier: "hello", content: content, trigger: trigger) 
    let center = UNUserNotificationCenter.current() 

    center.add(request) { (error : Error?) in 
     if let theError = error { 
      print("theError \(theError)") 
     } 
    } 
} 

이러한 기능을 활용하여 각 클래스에 import UserNotifications해야합니다.

자세한 정보 : User Notifications documentation.

+0

'center.delegate'에'self'를 사용하게하지 않습니다. – krish

+0

클래스가 UNUserNotificationCenterDelegate를 구현해야합니다 – Frankie

+0

+ Frankie 어떻게해야합니까? – krish

관련 문제