简体   繁体   中英

Handle tap on local notification when app is terminated - iOS - Swift

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM