首页 > 解决方案 > 如何在 appdelegate 中获取传递的通知详细信息

问题描述

我已经设法通过点击通知打开应用程序时获取通知详细信息,但是如果用户通过单击应用程序图标打开应用程序,是否有任何方法可以在应用程序内获取传递的通知详细信息

func applicationWillResignActive(_ application: UIApplication) {

        if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()
            center.getDeliveredNotifications { (notification) in
                print(notification.count) 

            }

        } else {
            // Fallback on earlier versions
        }

    }

在这里我得到了通知计数,但我不知道如何从这里获取通知的详细信息(用户信息)

标签: iosswiftnotificationsappdelegate

解决方案


如果您的应用程序被强制关闭(向上滑动)并且用户单击通知打开应用程序,您仍然可以在AppDelegate didFinishLaunchingWithOptions方法中获取此信息:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject]{
        //Here you can use the notification payload information.
    }

    return true
}

推荐阅读