I am trying to react on silent push messages. I registered my app for the two background modes in my plist file.
I am sending the following json to my app:
{
"aps": {
"content-available": 1
},
"Bla": "Blub"
}
If my app is in foreground, everything works. As I am receiving the push I execute a function in my code. If my app is in background and plugged to my Mac or ac, everythings fine as well... But if I unplug my device, the device is not getting or at least my function is not called anymore...Why is that?
The way I understand that silent notifications work is that they can be sent, the system may receive them, but may not act upon them depending on different factors.
A silent notification is used to give the illusion of multi-tasking on a device that is power efficient as well. My understanding is that there are a few factors they system uses to determine whether it should pass along a silent notification to an app, these include:
- Current Network conditions
- Current Power levels
- Application usage.
- User choice
- Possible power drain.
In your case the silent notification is working when in the foreground because you are the foremost application. The user could be doing a task which does not involve the content right away. But the content update could be useful. For example with Facebook, I may be writing a post on a friends wall, there are new stories for my feed. It is nice to have that updated whilst I am doing something else.
For the case of being connected to your computer - The device is charging doing the background task is not going to be a drain on the device battery.
To give more details for the factors:
Network Conditions
- If the device is on wi-fi it is more likely to allow for network calls, as this isn't as power intensive as using cellular data.
Current Power Levels
- This also ties in to network conditions.
- Has the notification been received at a point that the relevant radios are already on?
- This could also be tied in to current location based on the device history. If the location is determined to be "home" it can be assumed that the device is near a power source.
- Time can also be a factor in this. e.g. "What is the average time the device starts charging, is it close etc."
- Is the device already connected to a power source?
Application Usage
- How often does the user use your application, what pattern of usage is there?
- Has the notification been received at a time that makes sense to allow the app to update its content? A possible example could be, the user is likely to use the application in the next 10 mins to 1 hour. It could make sense to update the content. But if the user is likely to use the app in the next 2 mins or 2 hours. It may defer this as it is likely to either be user initiated soon or not going to be used soon, a waste of resources.
User Choice
- The user can always choose to stop background data refresh.
- The user can choose not to allow your app to use cellular data.
- Whilst outside the scope of your question, I think it should be noted, a silent notification should not be assumed to always come through and should not be a critical part of your application.
Possible Power Drain
- Compared to the other factors, I don't think this exists, but something to keep in mind.
- When you are doing this background task, what has power consumption been like in the past?
- If you are a good citizen, as power efficient as possible etc, you may be more likely to be called to update.
- But if you are not as power efficient as possible in the systems view when doing this task, you may not be notified as much.
These factors are not definitive, proven or stated from what I can tell. I am suggesting them as possible reasons based on WWDC talks, previous background task restrictions and my own assumptions. But I hope they help give an idea why your silent notifications are working in certain instances and not in others.