Apple Push Notification Service

In iPhoneOS 3, Apple introduced remote push notifications. The iPhone keeps a persistent TCP connection to the Apple push notification service ("APNS", sometimes written "APNs" in Apple docs). The server associated with an app can send a notification to Apple servers, which will then send it to the user's device to be displayed, even when the app is not running.

APNS uses a proprietary binary protocol for communication between the device and Apple, encrypted with TLS, over TCP port 5223 (although it can fall back to 443 if it hits firewalls). The protocol has nothing to do with XMPP, which uses the same port to establish SSL-encrypted client connections. Up to iOS 4, the protocol used command IDs  to. In iOS 5, the protocol was changed; it now uses command IDs starting at  and all fields in a message (called "items") have a consistent type-length-value encoding. This is also when it was introduced into macOS (Lion).

While every iOS version after that continued adding new commands and items, since iOS 10 there is another new push protocol known as "apnspack", which uses the same command and item IDs but encodes them in a completely different binary format. The client negotiates use of this new protocol with the protocol name "apns-pack-v1" in ALPN. The apnspack format is not yet documented in this page.

Message Structure
The format of the non-packed APNS protocol is as follows:


 * 1 byte message type ("command ID")
 * 4 byte payload length
 * items, all with
 * 1 byte type
 * 2 byte length
 * value

All integers are big-endian.

Example:
 * command ID (Connect)
 * 39 byte payload length
 * item 1
 * 32 byte length
 * value 1 (32-byte push token)
 * item 2
 * 1 byte length
 * value

Items may be repeated (multiple items with the same ID). For example, command  "push topics" has multiple items  "enabled topic", one for each topic hash to be enabled.

Commands
Note that some items are optional, either because they're only sent depending on certain conditions, or because they were introduced in a later iOS version and earlier ones don't send it. That will be documented in more detail later.

07 Connect
First command sent after SSL handshake is completed.

When a device is first activated, it doesn't have a push token yet. This command 07 is sent without a token, and the server returns a new token in its command 08 reply. In all future connections, the device sends the token in command 07, and the server doesn't return a token in command 08.


 * Direction: device to server
 * command ID:
 * items:
 * 32-byte push token
 * 1 byte "state" (value )
 * 4-byte flags, bitfield (example from iOS 12: )
 * 1 byte interface (0: cellular, 1: Wi-Fi)
 * cellular carrier name (or the string "WiFi")
 * OS version (example: 12.4.8)
 * OS build (example: 16G201)
 * hardware version (example: iPhone6,1)
 * certificate, contains the X.509 "device certificate" obtained during device activation
 * 17-byte nonce, consisting of 1 byte fixed, 8 bytes timestamp (milliseconds since Unix epoch), 8 bytes random
 * "signature", consisting of fixed bytes 01 01, followed by RSASSA-PKCS1-SHA1 signature of the nonce (0d) using the public key in the certificate (0c)
 * 2-byte int, possibly protocol version
 * 2-byte int, "redirect count"
 * 2-byte int, "DNS resolve time" in milliseconds
 * 2-byte int, "TLS handshake time" in milliseconds

08 Connect Response

 * Direction: server to device
 * command ID:
 * items:
 * status ( ok,   some error)
 * 32-byte push token (unless the device sent one in Connect)
 * 2-byte int, max message size (value )
 * unknown (value )
 * capabilities (bitfield)
 * 2-byte int, large message size
 * 8-byte int, server time, milliseconds since unix epoch
 * 2 bytes, geo region (country code)

The lowest significant bit in 'capabilities' seems to mean "dual channel support" (possibly related to the iPhone proxying Apple Watch notifications).

09 Push Topics

 * Direction: device to server
 * command ID:
 * items:
 * device's push token
 * 20-byte hash for enabled topics
 * 20-byte hash for disabled topics
 * 20-byte hash for "opportunistic" topics
 * 20-byte hash for "paused" topics

Note that there are multiple items with the same numeric ID, one for each topic hash.

"Topic hashes" are SHA-1 hashes of the topic name (a reverse-DNS string). This can be an internal topic name used by an Apple service, or for third party apps, the app bundle ID. For example, iMessage notifications have topic hash , which corresponds to.

0A Push Notification

 * Direction: server to device (for iMessage and other internal services, also the other way round)
 * command ID:
 * items:
 * topic hash or push token (see below)
 * topic hash or push token (see below)
 * notification payload
 * message ID (4 bytes)
 * expiry (32-bit UNIX timestamp)
 * timestamp (64-bit UNIX timestamp in nanoseconds)
 * unknown

Items 01 and 02 are swapped depending on the message direction (I don't think there's a good reason for this, probably a mistake that now they can't fix). If this is an incoming notification, from the server to the device, item 01 is the recipient's push token, and item 02 is the topic hash. If this is an outgoing message, from the device to the server, item 01 is the topic hash and item 02 is the sender's push token.

The notification payload can be either JSON (common for App Store apps) or binary plist (common for internal services).

0B Push Notification Ack

 * Direction: server to device (for iMessage and possibly others too, also the other way round)
 * command ID:
 * items:
 * message ID of the notification being acknowledged
 * status ( ok,   error)

0C Keep-Alive

 * Direction: device to server
 * command ID:
 * items:
 * connection method ("WiFi" or GSM MNC like "31038" for AT&T)
 * iOS version, e.g. "5.0"
 * iOS build number
 * device model, e.g. "iPhone2,1"
 * unknown (values like,   or  )

0D Keep-Alive Confirmation

 * Direction: server to device
 * command ID:
 * no items

0E No Storage

 * Direction: server to device
 * command ID:
 * items:
 * 32-byte push token

0F Flush

 * Direction: both
 * command ID:
 * items:
 * 2-byte integer indicating length of padding
 * padding: NULL-bytes, typical lengths are 64, 128, 256, 512