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

标签 ios swift push-notification nsusernotificationcenter

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

  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/

相关文章:

ios - 如何从 UIImagePicker 推送到 UIViewController

ios - 同一方法中的不同输入类型

iphone - 如何创建格式为 seconds :milliseconds? 的递增计时器

ios - 禁用特定时间段的通知

ios - 当我的应用程序在后台运行时,是否可以在通知上设置 PresentationsOptions?

ios - 将 View 推到标签栏 Controller 中的导航 Controller 上不会产生动画

Swift Mac OS - 如何在单击按钮后使用 AVPlayer 播放另一个视频/更改视频的 URL?

ios - cellForRowAtIndexPath 中的 dequeueReusableCellWithIdentifier 中的单元格直到滚动才显示

ios - Firebase Cloud Messages 不向 iOS 传送消息

android - 是什么导致了这个堆栈跟踪? (gtalkservice & jivesoftware.smack)