什么是静默推送通知?设备什么时候收到它?

What is Silent Push Notification? When does the device receive it?

我想清除通知栏中的本地通知。为此 实施后,我正在考虑使用静默推送通知。所以我想要 确认设备何时收到它以及我可以用它做什么?

当您发送静默推送通知时,如果应用已挂起,则系统会唤醒或启动您的应用并将其置于后台 运行 状态,然后再调用该方法,但如果应用已被用户终止手动则不会唤醒。

应用程序:didReceiveRemoteNotification:fetchCompletionHandler:

当您发送静默推送通知并且您的应用程序有最多 30 秒的挂钟时间来执行下载或任何其他类型的操作并调用指定的完成处理程序块时,将调用此方法。如果没有及时调用处理程序,您的应用将被暂停。

如果你想发送静默推送通知,那么你的通知负载应该是这样的:

{
    "aps" = {
        "content-available" : 1,
        "sound" : ""
    };
    // You can add custom key-value pair here...
}

它们可用于通知应用程序新内容,而无需用户通知。应用程序将在后台唤醒而不是显示通知警报(如果用户强行退出,iOS 不会自动启动您的应用程序)并且将调用 application:didReceiveRemoteNotification:fetchCompletionHandler:。然后您就有机会为用户透明地处理任何信息:

  • 下载一些内容
  • 同步一些元素,
  • 当用户重新打开应用程序时,直接在应用程序内通知用户

请注意,您的时间限制为 30 秒。

配置静默通知

To support silent remote notifications, add the remote-notification value to the UIBackgroundModes array in your Info.plist file. To learn more about this array, see UIBackgroundModes.

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

Configuring a Silent Notification

The aps dictionary can also contain the content-available property. The content- available property with a value of 1 lets the remote notification act as a silent notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.

For a silent notification, take care to ensure there is no alert, sound, or badge payload in the aps dictionary. If you don’t follow this guidance, the incorrectly-configured notification might be throttled and not delivered to the app in the background, and instead of being silent is displayed to the user