gpt4 book ai didi

ios - 如何在推送通知仅到达时获得通知,然后点击

转载 作者:行者123 更新时间:2023-11-28 07:45:36 24 4
gpt4 key购买 nike

当应用程序处于后台状态时,我必须启动两个不同的实验,

  1. 当通知到达用户但尚未点击时。 (这不是实现)
  2. 在所有通知中,用户点击通知的次数警报并打开应用程序。 (这就实现了)

我使用了以下方法:当用户通过点击警报消息来响应通知时,系统会调用以下方法并返回结果。

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

如果您的应用程序在前台并且收到通知,通知中心会调用以下方法将通知直接发送到您的应用程序。

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

最佳答案

您可以为此使用 application:didReceiveRemoteNotification:fetchCompletionHandler: 方法。当推送通知到达时,系统向用户显示通知并在后台启动应用程序(如果需要)以便它可以调用此方法。来自 Apple's Docs :

Implement this method if your app supports the remote-notification background mode. ... When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Use this method to download any data related to the push notification. When your method is done, call the block in the handler parameter.

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app.

关于above的使用方法:

It tells the app that a remote notification arrived that indicates there is data to be fetched.

关于ios - 如何在推送通知仅到达时获得通知,然后点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51134195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com