我正在从我的 iOS 应用程序发送
LocalNotification
。所有通知均已成功到达。我正在使用从 userInfo
函数接收的通知 didReceive response
处理这些通知,并且当应用程序处于后台或前台状态时不会遇到任何问题。
但是当应用程序处于终止(关闭)状态时,正如我们所知,将在
launchoptions
中获取通知 userInfo 作为 launchOptions?[.remoteNotification]
中 didFinishLaunchingWithOptions
函数的 AppDelegate
。但在这里我没有得到 userInfo
中的 launchOptions
。
请告诉我,项目设置中是否需要启用任何内容?请帮助我。
尝试 UNUserNotificationCenterDelegate。
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
//response.notification.request.content.userInfo
}
}
另外,在 didFinishLaucnhiningOptions 中
if let notificationData = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
//notificationData
}
我仍然遇到这个问题,我使用 willPresent 通知和 didReceive 响应方法,并且我们知道当应用程序处于前台或后台状态时它们会被调用,当应用程序终止时我单击本地通知,通知数据是进入 AppDelegate 中 didFinishLaucnhiningOptions 的 launchOptions 但启动选项中只有本地通知键,该键会引发弃用警告,有人找到解决方案吗?