When i reboot my phone without opening my app, and send a remote notification with 'content-available' bit, my app is not launched or invoke the delegate. But I can get the notification alert. The app was even launched for scheduled background fetch! Once I started my app by tap icon, the delegate is invoked as expected even if my app is in background or killed(not force quit).
Am I misunderstanding Apple's doc of this method or I'm missing something in my implementation?
It's running on iOS 9.3.1.
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. 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 push
notification arrives. However, the system does not automatically
launch your app if the user has force-quit it. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
Related
I have implemented application:didReceiveRemoteNotification:fetchCompletionHandler:, which is necessary to run after receiving a push notification. I have also turned on "Background Fetch" and "Remote notifications" background modes. And my push notifications include the content-available flag, set to 1.
If my app is in the Active, Background or Suspended state, this function gets called appropriately and the app temporarily moves into the Inactive state. However, if my app has been purged from the Suspended state due to a low memory issue (i.e. the user has opened a number of other apps since launching my app, a fairly common occurrence), it moves into the Not Running state. At that point, it doesn't call the didReceiveRemoteNotifications function.
The documentation for this function states:
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. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
The scenario where the user force quits the app and so the app doesn't call this function is acceptable. But what I'm seeing instead is if the system terminates the app for a fairly common reason, then this function doesn't get called.
I've also tried implementing application:didReceiveRemoteNotification:, the deprecated function, to see if that gets called when the app is in the Not Running state. It doesn't.
I'm implementing didReceiveRemoteNotification. I'm also saving these notifications locally. But I noticed if I'm at the home screen and don't open the notification, this function never gets called, and I can't save the notification. Any ideas?
You have to implement application:didReceiveRemoteNotification:fetchCompletionHandler: instead of application:didReceiveRemoteNotification:. See official documentation here:
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. 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. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
Also make sure to enable "remote notifications background mode", like stated in the documentation. Edit Info.plist and check the "Enable Background Modes" and "Remote notifications" check boxes:
According to Apple's documentation on remote notifications:
Discussion
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which
is called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. 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. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
In my case (iOS 7.1.1) remote notification is not delivered to the app after the app was killed (swipe up from Recent Apps List) and the phone was restarted. If I open the app, notifications get delivered as expected. What am I missing?
Edit: To avoid any misunderstanding. What I expect is the following flow:
User kills the app;
User restarts the phone;
App server sends a new message;
OS attempts to launch the app and deliver the notification.
The problem is that you are a developer, and your usage is not typical. What you do: Launch the app, swipe it out, turn off the phone (not standby, but turned off), reboot, enter your passcode, make your server send a notification. That doesn't work.
For some reason, notifications sent within 90 seconds or so after rebooting the phone are not received. Wait 90 seconds, then you send the notification, and it should be received. Since there is no relation between the time the phone of the user is rebooted and the time you send the notification, this is only a problem for developers and testers, not for real users.
From the article you linked, for the method you're referencing (application:didReceiveRemoteNotification:)
If the app is not running when a remote notification arrives, the
method launches the app and provides the appropriate information in
the launch options dictionary. The app does not call this method to
handle that remote notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
remote notification payload data and respond appropriately.
Are you checking for your remote notification payload in the options dictionary for the will/didFinishLaunchingWithOptions:?
Silent notifications work via Background modes and content available flag, but only if the app is in background.
Is it possible to know if/when the user received the notification, even when the app is suspended(not in background) ?
The system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
Here is the relevant section from the documentation:
application:didReceiveRemoteNotification:fetchCompletionHandler:
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. 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 push
notification arrives. However, the system does not automatically
launch your app if the user has force-quit it. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
In documentation for method
application:didReceiveRemoteNotification:fetchCompletionHandler:
said
Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. 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 push notification arrives.
I set 'background fetch' and 'Remote notifications' option in .plist and 'launch due to background fetch event' option in scheme settings.
If my app running foreground and received push, this method called and all seems ok.
But when i run my app from xCode and then move it to background mode (by pressing home button), it won't react on breakpoints or NSLogs in method above when push sent. System displays push notification, but i still can't do some actions when push arrives.
Is it possible to handle push notifications when app is running background?