我正在使用UNUserNotification 10中的新iOS框架。我可以看到如何添加操作按钮,但是当用户点击通知本身时,我该如何响应呢?在我的例子中,它将是一个带有文本的图像。
默认行为是打开应用程序。
UNUserNotification抽头而被打开,并且理想的情况下是带有有关通知监听的标识符信息?UNUserNotification文档建议设置UNUserNotificationCenter的委托,但我认为只有在应用程序运行时才能这样做。发布于 2017-05-23 18:45:53
如果尚未设置AppDelegate,则设置为UNUserNotificationCenterDelegate,并将以下内容添加到didReceive方法中:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = ["identifier":response.notification.request.identifier]
NotificationCenter.default.post(name: Notification.Name(rawValue: "userNotificationCenterDidReceiveResponse"), object: self, userInfo: userInfo)
completionHandler()
}然后,您可以注册该"userNotificationCenterDidReceiveResponse“通知,并对标识符执行您喜欢的任何操作。无论您的应用程序处于何种状态,这都可以工作,包括不运行。
如果这还不够清楚,我可以上传一个示例项目。
发布于 2017-05-25 12:03:40
使用UNUserNotificationCenter Delegates。
当应用程序在前台时使用这个:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
从notification获取要使用的值
当应用程序处于后台时:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
从response获取要使用的值。
希望这能帮上忙。
https://stackoverflow.com/questions/44079616
复制相似问题