简体   繁体   English

应用被杀死时是否收到 iOS 静默通知

[英]Is a iOS silent notification received when the app is killed

when sending a background push with "content-available": "1" , to an app that is killed by the user, the application is not launched into the background mode and the application:didReceiveRemoteNotification:fetchCompletionHandler: is not called as the Apple doc say :当发送带有"content-available": "1"的后台推送到被用户杀死的应用程序时,该应用程序不会启动到后台模式并且application:didReceiveRemoteNotification:fetchCompletionHandler:不被称为Apple doc说

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.但是,如果用户强制退出,系统不会自动启动您的应用程序。

My question is: Is there is any way to access this silent push payload the next time the user starts the application?我的问题是:有没有办法在用户下次启动应用程序访问这个静默推送有效负载?

I tried using the launchOptions of the didFinishLaunchingWithOptions method but they do not contain the push payload.我尝试使用didFinishLaunchingWithOptions方法的launchOptions ,但它们不包含推送有效负载。

NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

My use case is that I rely only on the push channel to receive data to the app but the app cannot pull them.我的用例是我只依靠推送通道来接收应用程序的数据,但应用程序无法拉取它们。

Short answer is: no, you cannot.简短的回答是:不,你不能。

You also won't be able to use VoIP pushes, the only option would be to use regular pushes with a push notification service extension.您也将无法使用 VoIP 推送,唯一的选择是使用带有推送通知服务扩展的常规推送。 Share a keychain between your app and this extension, save the push payload in the keychain when receiving a notification and retrieve it with your app when it enters foreground.在您的应用程序和此扩展程序之间共享一个钥匙串,在收到通知时将推送有效负载保存在钥匙串中,并在它进入前台时用您的应用程序检索它。 Downside is you will need to present a visual notification to the user, but it can be silent, and you can choose to present whatever text you want (the best option will depend on what your app does and what's the purpose of this notification).缺点是您需要向用户呈现视觉通知,但它可以是静音的,您可以选择呈现您想要的任何文本(最佳选择取决于您的应用程序的用途以及此通知的目的)。

You may use a VoIP Push message, see here:您可以使用 VoIP 推送消息,请参见此处:

Voice Over IP (VoIP) Best Practices IP 电话 (VoIP) 最佳实践

There are many advantages to using PushKit to receive VoIP pushes:使用 PushKit 接收 VoIP 推送有很多优点:

  • [...] [...]
  • Your app is automatically relaunched if it's not running when a VoIP push is received.如果您的应用程序在收到 VoIP 推送时未运行,则会自动重新启动。
  • [...] [...]

Be aware, that your app must have background mode with VoIP capability enabled, which may be an issue for app store approval if misused.请注意,您的应用程序必须具有启用 VoIP 功能的后台模式,如果滥用,这可能会成为应用程序商店批准的问题。

Looking at the documentation, it seems like you should implement this method:查看文档,您似乎应该实现此方法:

optional func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)可选的 func 应用程序(_ 应用程序:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult)-> Void)

Within that method, write your code to store the payload (userInfo).在该方法中,编写代码以存储有效负载 (userInfo)。 Maybe store it in the userDefaults temporarily.也许将它暂时存储在 userDefaults 中。 Then when the application launches, check to see if the payload is available.然后当应用程序启动时,检查有效负载是否可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM