正在远程关闭 iOS 推送通知

Dismissing iOS push notifications remotely

iOS 的 Gmail 应用程序可以接收推送通知,但该应用程序不能 运行(大多数电子邮件应用程序都可以)。

但是,当用户收件箱的未读计数变为零时,它也能够清除设备中的所有 Gmail 推送通知,即使该应用不是 运行.

这是一个示例序列: 1. 在您的 Gmail 帐户中收到一封新电子邮件。 2. iOS 设备显示新消息通知。 3. 转到Gmail 网站并打开邮件(将邮件标记为"read")。 4. iOS 设备上的通知被关闭。

注意:[[UIApplication sharedApplication] scheduledLocalNotifications] 仅提供本地通知,即在 iOS 应用本身内创建的通知。

就 Apple 的 documentation for APNS describes, there is no way to remotely launch an app into the background 而言,无法关闭远程通知。

那么,Gmail iOS 应用程序是如何做到这一点的?

iOS 中有一个 'silent push' 功能,允许您的应用程序在收到 UI-less 推送通知后在后台唤醒并自行更新。

Session 713 at WWDC 2014 对此进行了详细描述:

Silent notifications, they are just push payloads that are sent from your APNs server that, instead of presenting a user notification like an alert or a sound or a badge on the screen, iOS, when it receives that push, will instead wake up your app in the background so that your app can do some background image processing or information processing.

In this case, your app is fetching content from a server In this case, your app is fetching content from a server so that the next time the user happens to tap on your app icon and bring it to the foreground, that information is there and ready so nobody has to wait for a loading spinner to complete and all that other stuff.

通过使用 Parse 推送此有效负载,我也能够清除所有推送通知。我猜只要您提供 content-availablebadge,您应该也能做到。我不必在 AppDelegate 中编写任何其他代码,但我确实必须在项目目标 capabilities.

中打开推送通知
curl -X POST \
-H "X-Parse-Application-Id: xxxxxxxxxxx" \
-H "X-Parse-REST-API-Key: xxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
       "data": {
         "content-available": "1",
         "badge":"0",
         "sound":""
       },
       "where": {"something":"something_else"}
     }' \
https://api.parse.com/1/push

如果徽章编号已设置,请尝试此操作,

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

如果未设置,请尝试此操作

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

这将清除您的推送通知和本地通知。

的解决方案适用于批量删除推送通知。如果要删除特定的,请按照此操作。

要远程删除特定的推送通知 (PN),您需要在数据库中存储该 PN 的公共 apns-collapse-id。当你想删除它时,只需 send another silent push notification with the same apns-collapse-id

.