我正在使用新的UNUserNotification
iOS 10 中的框架。我可以看到如何添加操作按钮,但是当用户点击通知本身时我如何响应?就我而言,它将是带有一些文本的图像。
默认行为是应用程序打开。
我可以使用自定义代码来检测我的应用程序是否由于以下原因而被打开UNUserNotification
点击,并且最好包含有关点击的通知的标识符信息?
如果我的应用程序在后台运行或关闭,这些功能会起作用吗?UNUserNotification
文档建议设置委托UNUserNotificationCenter
,但我认为这仅在应用程序运行时才有效。
Set AppDelegate
to be UNUserNotificationCenterDelegate
如果您还没有添加以下内容didReceive
method:
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”通知,并使用该标识符执行任何您喜欢的操作。无论您的应用程序处于什么状态(包括未运行),这都有效。
如果这还不够清楚,我可以上传一个示例项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)