-1

I'm sending LocalNotification from my iOS app. All notifications are arriving successfully. I'm handling those notifications using notification userInfo which is receiving from didReceive response function and facing no issues when app is in either background or foreground state.

But when app is in terminated (close) state, as we know that will get that notification userInfo in launchoptions as launchOptions?[.remoteNotification] of didFinishLaunchingWithOptions function in AppDelegate. But here I'm not getting that userInfo in launchOptions.

Let me know, is there anything need to enable in project settings? Please help me.

Harsha
  • 760
  • 1
  • 7
  • 21

1 Answers1

1

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

Also, in didFinishLaucnhingOptions

if let notificationData = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
        //notificationData
    }
Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
  • Thanks for your answer. As you mentioned, I'm using `willPresent notification` and `didReceive response` methods and as we know they will get called when app is in foreground or background state. When app is in terminated state, we will get notification data in `didFinishLaucnhingOptions` as you said, but here I'm not getting that notification data in `launchOptions`. That's the issue. – Harsha Apr 11 '21 at 06:38
  • Hi @Harsha, Did you find any solution of it. I am facing the same problem. I am clicking on a local notification when app is terminated, notification data is getting in `launchOptions` of `didFinishLaucnhingOptions` in AppDelegate but there is only local notification key in launch options which is throwing warning of deprecation. Can you please let me know how to resolve this? Thanks – Shivam Tiwari Dec 06 '22 at 06:19