问题描述
我正在从我的 iOS 应用发送 localnotification
。所有通知都成功到达。我正在使用从 userInfo
函数接收的通知 didReceive response
处理这些通知,并且在应用处于后台或前台状态时不会遇到任何问题。
但是当应用程序处于终止(关闭)状态时,我们知道将在 launchoptions
中作为 launchOptions?[.remoteNotification]
的 didFinishLaunchingWithOptions
函数中的 AppDelegate
获取通知 userInfo。但在这里我没有在 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
}
}
此外,在 didFinishLaucnhingOptions 中
if let notificationData = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
//notificationData
}