应用被终止时是否收到 iOS 静默通知
Is a iOS silent notification received when the app is killed
当使用 "content-available": "1"
向被用户杀死的应用程序发送后台推送时,应用程序不会启动到后台模式,并且 application:didReceiveRemoteNotification:fetchCompletionHandler:
不会被调用,因为 Apple doc say:
Use this method to process incoming remote notifications for your app. [...]In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives.
However, the system does not automatically launch your app if the user has force-quit it.
我的问题是:有没有什么方法可以在用户下次启动应用程序时访问这个静默推送负载?
我尝试使用 didFinishLaunchingWithOptions
方法的 launchOptions
,但它们不包含推送负载。
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
我的用例是我仅依靠推送通道接收数据到应用程序,但应用程序无法拉取它们。
查看文档,似乎你应该实现这个方法:
可选功能应用程序(_应用程序:UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler 完成处理程序:@escaping (UIBackgroundFetchResult) -> Void)
在该方法中,编写代码来存储负载 (userInfo)。也许暂时将它存储在 userDefaults 中。然后当应用程序启动时,检查负载是否可用。
您可以使用 VoIP 推送消息,请参阅此处:
Voice Over IP (VoIP) Best Practices
There are many advantages to using PushKit to receive VoIP pushes:
- [...]
- Your app is automatically relaunched if it’s not running when a VoIP push is received.
- [...]
请注意,您的应用必须具有启用 VoIP 功能的后台模式,如果使用不当,这可能会成为应用商店批准的问题。
简短的回答是:不,你不能。
您也将无法使用 VoIP 推送,唯一的选择是使用带有推送通知服务扩展的常规推送。在您的应用程序和此扩展程序之间共享一个钥匙串,在收到通知时将推送有效负载保存在钥匙串中,并在您的应用程序进入前台时将其检索。
缺点是您需要向用户显示视觉通知,但它可以是无声的,并且您可以选择显示您想要的任何文本(最佳选择将取决于您的应用程序做什么以及此通知的目的是什么)。
当使用 "content-available": "1"
向被用户杀死的应用程序发送后台推送时,应用程序不会启动到后台模式,并且 application:didReceiveRemoteNotification:fetchCompletionHandler:
不会被调用,因为 Apple doc say:
Use this method to process incoming remote notifications for your app. [...]In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it.
我的问题是:有没有什么方法可以在用户下次启动应用程序时访问这个静默推送负载?
我尝试使用 didFinishLaunchingWithOptions
方法的 launchOptions
,但它们不包含推送负载。
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
我的用例是我仅依靠推送通道接收数据到应用程序,但应用程序无法拉取它们。
查看文档,似乎你应该实现这个方法:
可选功能应用程序(_应用程序:UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler 完成处理程序:@escaping (UIBackgroundFetchResult) -> Void)
在该方法中,编写代码来存储负载 (userInfo)。也许暂时将它存储在 userDefaults 中。然后当应用程序启动时,检查负载是否可用。
您可以使用 VoIP 推送消息,请参阅此处:
Voice Over IP (VoIP) Best Practices
There are many advantages to using PushKit to receive VoIP pushes:
- [...]
- Your app is automatically relaunched if it’s not running when a VoIP push is received.
- [...]
请注意,您的应用必须具有启用 VoIP 功能的后台模式,如果使用不当,这可能会成为应用商店批准的问题。
简短的回答是:不,你不能。
您也将无法使用 VoIP 推送,唯一的选择是使用带有推送通知服务扩展的常规推送。在您的应用程序和此扩展程序之间共享一个钥匙串,在收到通知时将推送有效负载保存在钥匙串中,并在您的应用程序进入前台时将其检索。 缺点是您需要向用户显示视觉通知,但它可以是无声的,并且您可以选择显示您想要的任何文本(最佳选择将取决于您的应用程序做什么以及此通知的目的是什么)。