简体   繁体   English

当应用程序终止时处理点击本地通知 - iOS - Swift

[英]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 .当应用程序终止时,我想处理本地通知的点击,但是方法application didFinishLaunchingWithOptions中没有启动选项,也没有调用方法application didReceive notification

I also tried to implement two methods of UNUserNotificationCenterDelegate :我还尝试实现UNUserNotificationCenterDelegate的两种方法:

1)Handles notification, when app is in background, but not terminated : 1)处理通知,当应用程序处于后台但未终止时

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
)

2)And next handles notification, when app is in foreground: 2)接下来处理通知,当应用程序在前台时:

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)操作系统启动您的应用程序(之前已终止),然后仍然将操作传递给此(功能 1)

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
)

You can get the notification object from response like what you did.您可以像您所做的那样从响应中获得notification object。 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.请记住在完成逻辑时调用completionHandler() And, you must set UNUserNotificationCenter.current().delegate = self , before you return true in并且,您必须设置UNUserNotificationCenter.current().delegate = self ,然后再return true

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. self这里是您的AppDelegate ,这意味着您将 UserNotification 的委托设置为实时 object 或初始化 object。它将能够处理上述委托 function 调用(功能 1)以获取您选择的推送通知的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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