2016-11-19 4 views
0

私はViewController.swiftで予定された通知を作成し、この通知に2つのアクションを追加する必要があります。 「通話」と「キャンセル」との両方が表示されます。 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

私はまだiOSの10での通知を使用していないので、私は学習として先に行って、それを考え出しました私の経験は、今私はあなたにそれを渡すことができます。

UILocalNotificationは、iOS 10では償却され、UserNotificationsフレームワークに置き換えられました。あなたのAppDelegate通知を表示し、ユーザからの許可を取得し、UNUserNotificationCenterDelegateでセンターのデリゲートを設定するには

アクションの取り扱い
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    // Play sound and show alert to the user 
    completionHandler([.alert,.sound]) 
} 

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, 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

関連する問題