Reputation: 13
I want to handle tap on local notification, when the app is terminated, but there are no launch options in method application didFinishLaunchingWithOptions
and no call of method application didReceive notification
.
I also tried to implement two methods of UNUserNotificationCenterDelegate
:
1)Handles notification, when app is in background, but not terminated:
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
)
2)And next handles notification, when app is in foreground:
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
)
Have no idea how to handle it...
Upvotes: 1
Views: 3287
Reputation: 542
When you tap on Push Notification on Notification Center. OS launches your app (terminated before) then STILL delivers the action to this (Function 1)
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
)
You can get the notification
object from response like what you did.
But you may need to save temporary this data from the selected push notification, then handle it later when app is completely active with user.
Remember to call completionHandler()
when you finish your logic. And, you must set UNUserNotificationCenter.current().delegate = self
, before you return true
in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool`.
self
here is your AppDelegate
, which means you set UserNotification's delegate to a live object or initialized object.
It will be able to handle the above delegate function call (Function 1) to get your selected push notification's data.
Upvotes: 2