繁体   English   中英

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

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

当应用程序终止时,我想处理本地通知的点击,但是方法application didFinishLaunchingWithOptions中没有启动选项,也没有调用方法application didReceive notification

我还尝试实现UNUserNotificationCenterDelegate的两种方法:

1)处理通知,当应用程序处于后台但未终止时

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

2)接下来处理通知,当应用程序在前台时:

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
)

不知道如何处理它...

当您点击通知中心的推送通知时。 操作系统启动您的应用程序(之前已终止),然后仍然将操作传递给此(功能 1)

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

您可以像您所做的那样从响应中获得notification object。 但是您可能需要从选定的推送通知中临时保存此数据,然后在应用程序完全与用户激活时处理它。

请记住在完成逻辑时调用completionHandler() 并且,您必须设置UNUserNotificationCenter.current().delegate = self ,然后再return true

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool`.

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

暂无
暂无

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

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