To receive silent push notifications in the background, I need to include content-available
payload into my APNS push notification, and have application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
implemented.
When I send a push notification, my app receives and handles it with the above method.
This works correctly and is straightforward to test when the app is running in foreground or background on my device.
I would like to test if my app is woken up correctly from scratch and does the above work. But how do I do this? How do I get my app into the state where it is started when it receives a silent push notification?
When the user force-kills the app, the notification is not delivered. This is also the case when I as a developer force-kill the app which I am testing. I assume it also applies when I force-stop the app running on my device from within Xcode.
Is there any way for me as a developer to terminate the app, which does not count as “force killing”, and would reliably start the app when it receives the background notification?
Stopping the app from Xcode does not count as force closed.
As much as I hate to recommend it, alternatively you could call exit(0)
from a button that will terminate the app without counting as force closed. Just make sure you don't ship your app with that code in it.
Also, if your app were to crash, that would also not be count as force closed.
But as for "reliably" launching the app for testing purposes, unfortunately there isn't a way to guarantee that.
If you want to know what happens when the app is launched due to a notification, you will see application(_:willFinishLaunchingWithOptions:)
and application(_:didFinishLaunchingWithOptions:)
called before application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
So perhaps you can simulate a launch by mocking the calls to the code which gets called from those functions.
Argun Tekant / DTS Engineer / Core Technologies