4

An iOS device (iPad or iPhone) receives notifications, even when the device is sleeping. Apple calls these “push notifications”.

How does the device receive these notifications? Are there any periodic (recurring) actions involved? If so, what actions are they and how often do they occur?

3
5

From the Apple Push Notification Service documentation:

Each device establishes an accredited and encrypted IP connection with APNs and receives notifications over this persistent connection.

This means it uses some sort of socket-based persistent connection. It could be its own proprietary protocol, or WebSocket, etc. The "periodic actions" you're asking about are usually implemented in these protocols at the lowest level possible using TCP/IP sockets and are being constantly polled for new data on the socket. When the data is finished being read off the socket, an event is fired off to go perform some action such as display a notification on the device.

To further explain, the way sockets work is your device makes a connection to a remote server, the remote server pushes data to your device and gets put into the socket's buffer, then your device reads the socket's buffer and does something with the data. The moment the data gets pushed to your device, your device will process it. This is because your device is checking for new data on the socket's buffer all the time. It tries to read data, and if it gets data, it sends it off to get processed and then tries to read data again; if it ever doesn't get data, it just tries to read data again - forever. If the connection to the remote server ever gets disconnected, it just tries to connect again until it succeeds.

The code documentation I linked earlier is taken from Boost's ASIO library which is widely used for sockets in C++. It's implementation and usage can be simplified as follows:

01 start program
02 try to connect to server by using a socket
03 if not connected to server, goto 02
04 try to read data on socket
05 if data was read, process data
06 goto 03

In step 03 above, the device knows when you're offline and won't attempt to connect again until you get back online (the device sends out an event when you connect to the internet, so it will all happen instantly).

When no data was read in step 04 above, it immediately tries to read it again without waiting (assuming you're still connected to the server). It takes a very small amount of processing power to repeatedly try to read data on the socket, so there's no need to worry about battery or wasted resources (see @malhal's answer here for a better explanation on this and also how it works when the device is sleeping).

13
  • 2
    @NicolasBarbulesco I updated my answer and explained how sockets work and how the device tries to reconnect to the server if the connection is lost. Mar 22 '16 at 16:45
  • 2
    @NicolasBarbulesco Is my answer clear enough now? Mar 28 '16 at 4:18
  • 2
    @NicolasBarbulesco The device knows when you're offline and won't connect until you get back online. The device sends out an event when you connect to the internet, so it will all happen instantly. When there is no data to read, it immediately tries to read it again without waiting. It takes a very small amount of processing power to keep reading repeatedly like that, so it doesn't kill your battery or waste resources. Jul 22 '16 at 16:18
  • 2
    I have added that information to my answer. Do you consider it to be complete now? Jul 28 '16 at 20:48
  • 1
    Seriously, this is the correct answer and much better explained than the information I found through other sources. @Nicolas, be mindful of chameleon questions.
    – grg
    Jul 29 '16 at 18:07
2

Yes the device CPU is asleep but the cellular modem has another CPU called the baseband processor which keeps running, it monitors for incoming packets that match a certain filter and then wake up the CPU using an interrupt and allows it to process the incoming data. This is trick taken from Microsoft's Pocket PC direct-push, which was an improvement upon Blackberry's implementation that required telco-side BB infrastructure.

1
  • I did not know this! Thanks! So it's similar to what I described, except for it's the baseband processor that's doing the bulk of the work and not the regular CPU? What I was describing in my answer was just more of how socket polling in general works since that's what I understood was being asked (mainly about push notifications in general and that the sleeping part was just auxiliary information). Dec 6 '16 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .