How to get delivered notification details in appdelegate

Viewed 1471

i have managed to get the notification details when app opens by tapping the notification but if the user is opening the app by just clicking the app icon is there any method to get the delivered notification details inside the app

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
        }

    }

here i have getting the notification count but i dont no how to get the details (userinfo) of the notification from here

2 Answers

i have found the answer

func applicationDidBecomeActive(_ application: UIApplication) {
        if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()


            center.getDeliveredNotifications { (receivedNotifications) in


                for notification in receivedNotifications {
                    let content = notification.request.content
                    print(" Body \(content.body)")
                    print(" Title \(content.title)")
                    print(content.userInfo as NSDictionary)

                    self.session.saveCallRequest(content.userInfo as NSDictionary)

                }

            }
    }

In case your app was forced closed (swiped up) and the user click on the notification to open the app, you can still get this information in the AppDelegate didFinishLaunchingWithOptions method:

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