当推送通知到达(未收到)设备上时调用委托
Delegate called when push notification arrives (not received) on a device
我知道当推送通知到达设备时(应用程序没有打开它),没有办法检查(我可能是错的)。但是在我的应用程序中有这样一种情况,必须知道通知已经到达设备。
我在应用程序中有多个选项卡,其中 2 个选项卡根据推送通知实现徽章。所以基本上有两种不同类型的推送通知。
假设如果一种类型的通知到达设备并且用户选择不查看该通知。这样,特定 选项卡的徽章计数将增加 1。但问题是,哪一个。因为当时,我打开应用程序,我没有信息,通知类型已经到达。或者更准确地说,要增加哪个标签徽章计数。
所以简而言之,我怎么知道通知已到达设备(未收到)?
只有当用户通过点击通知打开应用或应用处于打开状态时,您才能获取通知负载信息。在这两种情况下,didReceiveRemoteNotification 都会调用。除了这些情况,您还可以在应用程序打开时调用包含通知计数信息的服务器上的服务。
您可以将一些参数添加到接收到推送通知的 json
数据中,例如 "type":1
以识别通知类型。参见 Examples of JSON Payloads。但是来自 API 的通知加载计数优于将每种类型的通知计入应用程序。
您可以通过为推送通知使用后台获取模式来解决此问题。将您的特殊徽章编号存储到某种持久存储(可能是用户默认值)并在您打开应用程序时使用该值。
对于后台抓取,请查看以下问题:
Will iOS launch my app into the background if it was force-quit by the user?
但是请注意,用户可以选择退出后台提取。
所以基本上,总结其他人所说的内容,您将有两个用户案例。但是在您需要在有效负载中指定必须递增的徽章之前(如@Andrew 所建议的)。
以下是用户案例:
- 应用可以调用
didReceiveRemoteNotification
。在这种情况下,这意味着该应用程序处于后台、前台或通过点击通知启动。您将必须实施您的逻辑来处理这种情况。只需解析您的通知的有效负载(在 json 中)并确定您应该增加哪个徽章。
- 应用程序无法调用
didReceiveRemoteNotification
。在这种情况下,用户已经终止了您的应用程序,或者设备已关闭,或者用户为您的应用程序设置了阻止获取后台功能。要处理这种情况,您需要在应用程序和服务器中添加更多逻辑。
您可以做的是在服务器端存储所有已发送的待处理通知。一旦用户设备上的应用程序收到 AND 解析通知(didReceiveRemoteNotification
被调用),您将向服务器发出新的调用,要求从待处理的数据库中删除通知.最后,调用你的服务器,在你的应用程序启动时请求所有未决通知,这样在你的应用程序被终止或设备关闭的情况下,当用户再次打开应用程序时,他将收到他的所有通知错过了。您还可以在服务器上设置一个计时器,每隔 x days/hours 再次发送一次通知(这不是真正的建议,您不希望用户删除您的应用程序,因为您在向他发送垃圾邮件)。
也正如其他人所说,相信您的服务器比通知本身要好得多。上面的逻辑用于我最近开发的类似 Tinder 的应用程序,以显示用户有多少匹配项;)
无法使用 public API 获取此信息。
来自Local and Remote Notifications Programming Guide:
Let’s review the possible scenarios that can arise when the system
delivers a local notification or a remote notification for an app.
The notification is delivered when the app isn’t running in the
foreground. In this case, the system presents the notification,
displaying an alert, badging an icon, perhaps playing a sound, and
perhaps displaying one or more action buttons for the user to tap.
The user taps a custom action button in an iOS 8 notification. In this
case, iOS calls either
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
or
application:handleActionWithIdentifier:forLocalNotification:completionHandler:.
In both methods, you get the identifier of the action so that you can
determine which button the user tapped. You also get either the remote
or local notification object, so that you can retrieve any information
you need to handle the action.
The user taps the default button in the alert or taps (or clicks) the
app icon. If the default action button is tapped (on a device running
iOS), the system launches the app and the app calls its delegate’s
application:didFinishLaunchingWithOptions: method, passing in the
notification payload (for remote notifications) or the
local-notification object (for local notifications). Although
application:didFinishLaunchingWithOptions: isn’t the best place to
handle the notification, getting the payload at this point gives you
the opportunity to start the update process before your handler method
is called.
For remote notifications, the system also calls the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method of the app delegate.
If the app icon is clicked on a computer running OS X, the app calls
the delegate’s applicationDidFinishLaunching: method in which the
delegate can obtain the remote-notification payload. If the app icon
is tapped on a device running iOS, the app calls the same method, but
furnishes no information about the notification.
如您所见,当应用程序从终止状态或未停靠状态启动时,仅检查点击通知。
另外一个重点是:
Quality of Service
Apple Push Notification service includes a default Quality of Service
(QoS) component that performs a store-and-forward function. If APNs
attempts to deliver a notification but the device is offline, the
notification is stored for a limited period of time, and delivered to
the device when it becomes available. Only one recent notification for
a particular app is stored. If multiple notifications are sent while
the device is offline, the new notification causes the prior
notification to be discarded. This behavior of keeping only the newest
notification is referred to as coalescing notifications.
If the device remains offline for a long time, any notifications that
were being stored for it are discarded.
所以,长话短说,推送通知的传递和存在是非常值得期待的,但不能保证。此外,推送通知在设备上的路径不能以编程方式控制- 作为一个应用程序,您在设备上订阅了 APNS 守护程序,并且在推送通知方面依赖于它。
这就是为什么,如果您的应用程序有一些业务逻辑(在您的情况下,计数器更新),您不应该依赖推送通知。您应该使用更可靠/可控的机制在您的应用程序和后端之间同步数据。通过使用 REST 并在我的应用程序的问题域中指定 Notification
实体,我已经实现了几次这个目标 - 用户可以通过 REST API 获取通知,将它们标记为 read/unread,删除它们等。
我知道当推送通知到达设备时(应用程序没有打开它),没有办法检查(我可能是错的)。但是在我的应用程序中有这样一种情况,必须知道通知已经到达设备。
我在应用程序中有多个选项卡,其中 2 个选项卡根据推送通知实现徽章。所以基本上有两种不同类型的推送通知。
假设如果一种类型的通知到达设备并且用户选择不查看该通知。这样,特定 选项卡的徽章计数将增加 1。但问题是,哪一个。因为当时,我打开应用程序,我没有信息,通知类型已经到达。或者更准确地说,要增加哪个标签徽章计数。 所以简而言之,我怎么知道通知已到达设备(未收到)?
只有当用户通过点击通知打开应用或应用处于打开状态时,您才能获取通知负载信息。在这两种情况下,didReceiveRemoteNotification 都会调用。除了这些情况,您还可以在应用程序打开时调用包含通知计数信息的服务器上的服务。
您可以将一些参数添加到接收到推送通知的 json
数据中,例如 "type":1
以识别通知类型。参见 Examples of JSON Payloads。但是来自 API 的通知加载计数优于将每种类型的通知计入应用程序。
您可以通过为推送通知使用后台获取模式来解决此问题。将您的特殊徽章编号存储到某种持久存储(可能是用户默认值)并在您打开应用程序时使用该值。
对于后台抓取,请查看以下问题: Will iOS launch my app into the background if it was force-quit by the user?
但是请注意,用户可以选择退出后台提取。
所以基本上,总结其他人所说的内容,您将有两个用户案例。但是在您需要在有效负载中指定必须递增的徽章之前(如@Andrew 所建议的)。 以下是用户案例:
- 应用可以调用
didReceiveRemoteNotification
。在这种情况下,这意味着该应用程序处于后台、前台或通过点击通知启动。您将必须实施您的逻辑来处理这种情况。只需解析您的通知的有效负载(在 json 中)并确定您应该增加哪个徽章。 - 应用程序无法调用
didReceiveRemoteNotification
。在这种情况下,用户已经终止了您的应用程序,或者设备已关闭,或者用户为您的应用程序设置了阻止获取后台功能。要处理这种情况,您需要在应用程序和服务器中添加更多逻辑。 您可以做的是在服务器端存储所有已发送的待处理通知。一旦用户设备上的应用程序收到 AND 解析通知(didReceiveRemoteNotification
被调用),您将向服务器发出新的调用,要求从待处理的数据库中删除通知.最后,调用你的服务器,在你的应用程序启动时请求所有未决通知,这样在你的应用程序被终止或设备关闭的情况下,当用户再次打开应用程序时,他将收到他的所有通知错过了。您还可以在服务器上设置一个计时器,每隔 x days/hours 再次发送一次通知(这不是真正的建议,您不希望用户删除您的应用程序,因为您在向他发送垃圾邮件)。
也正如其他人所说,相信您的服务器比通知本身要好得多。上面的逻辑用于我最近开发的类似 Tinder 的应用程序,以显示用户有多少匹配项;)
无法使用 public API 获取此信息。
来自Local and Remote Notifications Programming Guide:
Let’s review the possible scenarios that can arise when the system delivers a local notification or a remote notification for an app.
The notification is delivered when the app isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound, and perhaps displaying one or more action buttons for the user to tap.
The user taps a custom action button in an iOS 8 notification. In this case, iOS calls either application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forLocalNotification:completionHandler:. In both methods, you get the identifier of the action so that you can determine which button the user tapped. You also get either the remote or local notification object, so that you can retrieve any information you need to handle the action.
The user taps the default button in the alert or taps (or clicks) the app icon. If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.
For remote notifications, the system also calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method of the app delegate.
If the app icon is clicked on a computer running OS X, the app calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload. If the app icon is tapped on a device running iOS, the app calls the same method, but furnishes no information about the notification.
如您所见,当应用程序从终止状态或未停靠状态启动时,仅检查点击通知。
另外一个重点是:
Quality of Service
Apple Push Notification service includes a default Quality of Service (QoS) component that performs a store-and-forward function. If APNs attempts to deliver a notification but the device is offline, the notification is stored for a limited period of time, and delivered to the device when it becomes available. Only one recent notification for a particular app is stored. If multiple notifications are sent while the device is offline, the new notification causes the prior notification to be discarded. This behavior of keeping only the newest notification is referred to as coalescing notifications.
If the device remains offline for a long time, any notifications that were being stored for it are discarded.
所以,长话短说,推送通知的传递和存在是非常值得期待的,但不能保证。此外,推送通知在设备上的路径不能以编程方式控制- 作为一个应用程序,您在设备上订阅了 APNS 守护程序,并且在推送通知方面依赖于它。
这就是为什么,如果您的应用程序有一些业务逻辑(在您的情况下,计数器更新),您不应该依赖推送通知。您应该使用更可靠/可控的机制在您的应用程序和后端之间同步数据。通过使用 REST 并在我的应用程序的问题域中指定 Notification
实体,我已经实现了几次这个目标 - 用户可以通过 REST API 获取通知,将它们标记为 read/unread,删除它们等。