如何处理iOS 10中的用户通知操作

所以我可以安排这样的通知;

//iOS 10 Notification if #available(iOS 10.0, *) { var displayDate: String { let dateFormatter = DateFormatter() dateFormatter.dateStyle = DateFormatter.Style.full return dateFormatter.string(from: datePicker.date as Date) } let notif = UNMutableNotificationContent() notif.title = "I am a Reminder" notif.subtitle = "\(displayDate)" notif.body = "Here's the body of the notification" notif.sound = UNNotificationSound.default() notif.categoryIdentifier = "reminderNotification" let today = NSDate() let interval = datePicker.date.timeIntervalSince(today as Date) let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false) let request = UNNotificationRequest(identifier: "reminderNotif", content: notif, trigger: notifTrigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in if error != nil { print(error) // completion(Success: false) } else { //completion(Sucess: true) } }) } 

我已经在appDelegate要求权限,并且通知使用通知扩展与我的自定义视图显示正常。

我在通知类别的appDelegate添加了通知操作; 这些也出现了。

 //Notifications Actions private func configureUserNotifications() { if #available(iOS 10.0, *) { let tomorrowAction = UNNotificationAction(identifier: "tomorrowReminder", title: "Remind Me Tomorrow", options: []) let dismissAction = UNNotificationAction(identifier: "dismissReminder", title: "Dismiss", options: []) let category = UNNotificationCategory(identifier: "reminderNotification", actions: [tomorrowAction, dismissAction], intentIdentifiers: [], options: [.customDismissAction]) UNUserNotificationCenter.current().setNotificationCategories([category]) } else { // Fallback on earlier versions } } 

我在通知扩展.plist文件中设置了相同的类别。 在通知扩展中,当用户点击一个动作时,我有以下几点改变文本。

  //Handle Notification Actions And Update Notification Window private func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) { if response.actionIdentifier == "tomorrowReminder" { print("Tomrrow Button Pressed") subLabel.text = "Reminder For Tomorrow" subLabel.textColor = UIColor.blue done(.dismissAndForwardAction) } if response.actionIdentifier == "dismissReminder" { print("Dismiss Button Pressed") done(.dismiss) } else { print("Else response") done(.dismissAndForwardAction) } } 

然而,文字并没有改变,也没有任何声明被调用。

在AppDelegate中,我有以下;

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self configureUserNotifications() } } extension AppDelegate: UNUserNotificationCenterDelegate { @available(iOS 10.0, *) private func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) } @available(iOS 10.0, *) private func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { print("Recieved Action For \(response.actionIdentifier)") if response.actionIdentifier == "tomorrowReminder" { print("Tomorrow Reminder") //Set new reminder for tomorrow using the notification content title completionHandler() } if response.actionIdentifier == "dismissReminder" { print("Dismiss Reminder...") completionHandler() } } } 

这两个函数都不是在appDelegate调用的。 我不确定更新扩展视图的问题是否与应用程序委托相关。 我不这么认为,我跟着苹果的WWDCvideo以及其他的教程,看看这个文档的API,搞不清楚;

  • 为什么通知扩展文本标签不更新?
  • 为什么appDelegate中的函数没有被调用?
  • 如何使用应用程序委托中的通知内容来执行操作?

PS:在过去的几周里,我花了很多时间研究,试图弄清楚这个问题,看起来相当简单,我不确定我错过了什么。 我知道我不是唯一有这些问题的人。

我没有检查你的整个代码,但至less,这些函数头需要改变如下:

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { func didReceive(_ response: UNNotificationResponse, completionHandler done: @escaping (UNNotificationContentExtensionResponseOption) -> Void) { 

简单的规则: 删除private ,添加@escaping

您可能从Xcode收到了错误的build议,但是将它们设置为private ,Objective-C入口点不会生成。 iOS运行时在内部使用Objective-Cselect器,所以它不能find你的方法,因此它们不被执行。