134

How do iOS "push" notifications get delivered to a particular device without that device needing to poll a server?

For example, let's say I have received a new message on Facebook. Facebook notifies Apple that my device should receive a notification as such. But how does Apple know which device/IP to push the message to?

5
  • 3
    @Wain I am looking for a detailed explanation at a technical level. That article is extremely high level. So my device maintains an open connection with Apple's push servers at all times?
    – Andy Hin
    Commented Jun 23, 2013 at 15:58
  • Im more familiar with android push, but typically, when you install the app it registers the device with a unique ID with Apple. Then when facebook sends a message to apple, apple will forward it to the devices unique ID. Commented Jun 23, 2013 at 15:58
  • 9
    @Pjack yep, but with my device constantly changing different cell towers, different wifi networks, how does Apple know the IP for my unique ID?
    – Andy Hin
    Commented Jun 23, 2013 at 16:00
  • @whydna Thanks for asking this question. I wanted to understand precisely the same thing. I believe this answers my question here: stackoverflow.com/questions/18859732/… Commented Sep 18, 2013 at 9:53
  • @AndyHin The device OS (iOS) knows which APNs server(s) are valid. The device is connecting to these servers and keeping a connection open. This connection is likely associated with the device-id and other meta-data (phone number). When a message needs to be "pushed", the APNs finds the right connection and sends it down that already-open connection. So the APNs does not try to establish a connection to a device, it's always the device that opens that connection. One connection per device. Commented Aug 17, 2020 at 3:51

5 Answers 5

121

Each device can be updated with data using their own unique device tokens. This picture explains everything . .

enter image description here

9
  • Any idea what is the usage of the Device Token? Is it used for encrypting communication between the APNS server and the App or elsewhere?
    – Mugen
    Commented Apr 27, 2016 at 8:15
  • 1
    @Mugen: Device token is used by APNS to identify which device it is supposed to forward the payload sent by provider!
    – D4ttatraya
    Commented Jan 6, 2017 at 13:24
  • 1
    @Karan - Is there a distinction between notifications which are for a user (e.g. "Hey user you have a message!") and those that are meant for to wake up an app and silently tell it to do some background processing?
    – Howiecamp
    Commented Feb 23, 2017 at 0:47
  • @Howiecamp If you are using push notification, you will get notified whenever a notification has been received if the app is running. Also the active notification details are accessible , when user taps the notification from notification center and launches the app. During this time you can perform the processes that you wish to do. Commented May 16, 2017 at 9:17
  • @KaranAlangat, How do Badge Count handled here? How server knows about it?
    – GvSharma
    Commented Jul 20, 2018 at 11:27
68

It was too much for me to put in a comment so.

From the documentation.

Apple Push Notification service (APNs) propagates push notifications to devices having applications registered to receive those notifications. Each device establishes an accredited and encrypted IP connection with the service and receives notifications over this persistent connection. Providers connect with APNs through a persistent and secure channel while monitoring incoming data intended for their client applications. When new data for an application arrives, the provider prepares and sends a notification through the channel to APNs, which pushes the notification to the target device..

I suggest reading the documentation for more information and how to use and configure. It's all there.

Push Notifications

9
  • 7
    So Apple maintains a static IP to your device? Commented Jun 12, 2014 at 8:39
  • I was wondered when understood that my server have to send a notification to APNS FOR EACH TARGET DEVICE! :(
    – Vladimir
    Commented Jun 19, 2014 at 9:37
  • 5
    @CMCDragonkai The device establishes the connection, not the server. Commented Jun 22, 2016 at 1:08
  • @Panama jack hello sir how long will the message remain in the server if i put note.expiry = Math.floor(Date.now() / 1000) + 43200 sec than will it remain up to 12 hrs . i just want to know does expiry time has limit or it goes our way.
    – kisor
    Commented Dec 6, 2016 at 5:27
  • 8
    so the devices maintain a long polling/socket kind of connection always with APN service? If such is the case then how does APN service handle so many connections - If a server can hold 50k connections and there are 500 million users always, service at least needs 10000 servers right. Is this a practical scenario?
    – AV94
    Commented Jul 20, 2018 at 11:16
28

I created an infographic to explain the workflow of push notifications. Hope this is helpful.

enter image description here

1
  • Great infographic. However, it misses final step 6: APNS send notification to the device that APNS knows as connected to the token.
    – not2savvy
    Commented Jun 23, 2021 at 10:51
15

Device does not keep polling the server for the push notifications.

To keep it simple, consider an iPhone is connected to internet. On connecting to internet iPhone establishes connection to Apple Push Notifications server this connection is open connection which means data can be thrown to iPhone from server the moment data arrives to server.

Apple does not use HTTP protocol for Push notifications but if you understand HTTP Protocol its almost a similar methodology.

http://en.wikipedia.org/wiki/Push_technology#HTTP_server_push

1
  • What do they use if not http?
    – Howiecamp
    Commented Feb 23, 2017 at 5:59
2

There is a really nice exaplanation of push notifications in this article.

In iOS, apps can’t do a lot in the background. Apps are only allowed to do limited set of activities so battery life is conserved.

But what if something interesting happens and you wish to let the user know about this, even if they’re not currently using your app?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.