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

发布于 2025-01-30 16:19:20 字数 800 浏览 2 评论 0原文

我想在终止应用程序时处理点击本地通知,但是方法应用程序中没有启动选项dodfinishlaunchingwithoptions ,而方法application diDReceive didreceive noce

我还尝试实现两种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
)

不知道如何处理...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2025-02-06 16:19:20

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

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

您可以从响应中获得Notification对象,就像您所做的那样。
但是,您可能需要从所选的推送通知中保存临时数据,然后在用户完全活跃时将其处理。

请记住,完成逻辑后,请致电positionhandler()。而且,您必须设置unusernotificationcenter.current()。delegate = self,在您返回true in

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

self以下是您的appdelegate < /code>,这意味着您将Usernotification的委派设置为实时对象或初始化对象。
它将能够处理上述委托函数调用(功能1)以获取所选的推送通知数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文