我正在开发一个应用程序,可以安排本地通知并保存用户信息。这部分是没问题的。
但是当应用程序关闭时,如果出现通知并且用户单击,则该方法不会被调用,我无法处理用户信息。
我看到有一种使用UNUserNotificationCenter
接收通知的新方法。但也不能正常工作。
我尝试了以下方法,但没有成功:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let yourData = userInfo["yourKey"] as? String {
// Handle your data here, pass it to a view controller etc.
}
}
这是我在AppDelegate中的实现方式:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let lNotification = UILocalNotification()
lNotification.userInfo = response.notification.request.content.userInfo
// Handle your data here, pass it to a view controller etc.
}
有人能帮我吗?我看了所有相关的问题,但没有找到任何有用的答案。