我如何响应点击 UNUserNotification?
How do I respond to a tap on a UNUserNotification?
我在 iOS 10 中使用新的 UNUserNotification
框架。我可以看到如何添加操作按钮,但是当用户点击通知本身时我该如何响应?在我的例子中,它将是一张带有一些文字的图片。
默认行为是应用程序打开。
我能否使用自定义代码来检测我的应用程序是否因 UNUserNotification
点击而被打开,最好带有关于点击的通知的标识符信息?
如果我的应用 运行 在后台运行或已关闭,这些功能是否有效? UNUserNotification
文档建议设置 UNUserNotificationCenter
的委托,但我认为这仅在应用程序为 运行.
时有效
如果您还没有将 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" 通知并使用标识符做任何您喜欢的事情。无论您的应用处于什么状态,这都有效,包括不 运行.
如果这还不够清楚,我可以上传一个示例项目。
使用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
获取要使用的值。
希望这会有所帮助。
我在 iOS 10 中使用新的 UNUserNotification
框架。我可以看到如何添加操作按钮,但是当用户点击通知本身时我该如何响应?在我的例子中,它将是一张带有一些文字的图片。
默认行为是应用程序打开。
我能否使用自定义代码来检测我的应用程序是否因
UNUserNotification
点击而被打开,最好带有关于点击的通知的标识符信息?如果我的应用 运行 在后台运行或已关闭,这些功能是否有效?
UNUserNotification
文档建议设置UNUserNotificationCenter
的委托,但我认为这仅在应用程序为 运行. 时有效
如果您还没有将 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" 通知并使用标识符做任何您喜欢的事情。无论您的应用处于什么状态,这都有效,包括不 运行.
如果这还不够清楚,我可以上传一个示例项目。
使用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
获取要使用的值。
希望这会有所帮助。