Is a iOS silent notification received when the app is killed

牧云@^-^@ 提交于 2020-05-15 08:45:05

问题


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:

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.

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.


回答1:


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. 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).




回答2:


You may use a VoIP Push message, see here:

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.
  • [...]

Be aware, that your app must have background mode with VoIP capability enabled, which may be an issue for app store approval if misused.




回答3:


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)

Within that method, write your code to store the payload (userInfo). Maybe store it in the userDefaults temporarily. Then when the application launches, check to see if the payload is available.



来源:https://stackoverflow.com/questions/42526439/is-a-ios-silent-notification-received-when-the-app-is-killed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!