【问题标题】:Local Notification userinfo not getting in launch options本地通知用户信息未进入启动选项
【发布时间】:2021-07-06 05:51:10
【问题描述】:

我正在从我的 iOS 应用程序发送 LocalNotification。所有通知都已成功到达。我正在使用从didReceive response 函数接收的通知userInfo 处理这些通知,并且当应用程序处于后台或前台状态时不会遇到任何问题。

但是当应用程序处于终止(关闭)状态时,正如我们所知,launchoptions 中的通知 userInfo 将作为 didFinishLaunchingWithOptions 中的 launchOptions?[.remoteNotification] 函数在 AppDelegate 中获得。但在这里我没有在launchOptions 中得到userInfo

告诉我,项目设置中是否需要启用任何内容?请帮帮我。

【问题讨论】:

    标签: ios swift localnotification unusernotificationcenter userinfo


    【解决方案1】:

    试试 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
        }
    

    【讨论】:

    • 感谢您的回答。正如您所提到的,我正在使用willPresent notificationdidReceive response 方法,我们知道当应用程序处于前台或后台状态时它们会被调用。当应用程序处于终止状态时,我们将在didFinishLaucnhingOptions 中获取通知数据,如您所说,但这里我没有在launchOptions 中获取通知数据。这就是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多