iOS Apple push notification service (APNS)

Divesh Singh
4 min readMay 5, 2020

iOS supports two types of notifications: Local and Push.

Local notifications are registered and scheduled by the application itself.

Push notifications triggered by another service and it is for multiple devices at the same time.

The app owner takes the responsibility to send the notification.

It can be done fire at any times or scheduled Tims

message can be customized or a default message

the provider will send the message to APNS Server and APNS will push into targeted devices.

Provider >> APN servers >> Target Devices

there are two kinds of remote notifications:

Sandboxed that can be used in the development stage so it’s possible to test notifications.

Live that are meant to be used only in the production stage

Working push notification in the development stage with the SandBoxed, mean it will work with the live scenario as well.

For regular remote notifications, the maximum size is 4KB (4096 bytes) in the case of HTTP/2 request.

For regular remote notifications, the maximum size is 2KB (2048 bytes) in the case of legacy APN's binary interface.

For Voice over Internet Protocol (VoIP) notifications, the maximum size is 5KB (5120 bytes)

APNs refuse notifications whose payload exceeds the maximum allowed size.

Generating a Remote Notification

Send notifications to the user’s device with a JSON payload.

Overview

Remote notifications convey important information to the user in the form of a JSON payload. The payload specifies the types of user interactions (alert, sound, or badge) that you want to be performed, and includes any custom data your app needs to respond to the notification.

A basic remote notification payload includes Apple-defined keys and their custom values. You may also add custom keys and values specific to your notifications. Apple Push Notification service (APNs) refuses a notification if the total size of its payload exceeds the following limits:

Create the JSON Payload

Specify the payload for a remote notification using a JSON dictionary. Inside this dictionary, include an aps key whose value is a dictionary containing one or more additional Apple-defined keys, listed in Table 1. You can include keys instructing the system to display an alert, play a sound, or badge your app’s icon. You can also instruct the system to handle the notification silently in the background. For more information, see Pushing Background Updates to Your App.

In addition to the Apple-defined keys, you may add custom keys to your payload to deliver small amounts of data to your app, notification service app extension, or notification content app extension. Your custom keys must have values with primitive types, such as dictionary, array, string, number, or Boolean. Custom keys are available in the userInfo dictionary of the UNNotificationContent object delivered to your app.

Typically, you use custom keys to help your code process the notification. For example, you might include an identifier string that your code can use to look up app-specific data. Add app-specific keys as peers of the aps dictionary.

Listing 1 shows a notification payload that displays an alert message inviting the user to play a game. If the category key identifies a previously registered UNNotificationCategory object, action buttons are added to the alert. For example, the category here includes a play action to start the game immediately. The custom gameID key contains an identifier that the app can use to retrieve the game invitation.

Listing 1

A remote notification payload for showing an alert

{ “aps” : { “alert” : { “title” : “Game Request”, “subtitle” : “Five Card Draw” “body” : “Bob wants to play poker”, }, “category” : “GAME_INVITATION” }, “gameID” : “12345678” }

Listing 2 shows a notification payload that badges the app’s icon and plays a sound. The specified sound file must be on the user’s device already, either in the app’s bundle or in the Library/Sounds folder of the app’s container. The messageID key contains app-specific information for identifying the message that caused the notification.

A remote notification payload for playing a sound

{ “aps” : { “badge” : 9 “sound” : “bingbong.aiff” }, “messageID” : “ABCDEFGHIJ” }

Legacy Notification Format

The Binary Interface and Notification Format

The binary interface employs a plain TCP socket for binary content that is streaming in nature. For optimum performance, batch multiple notifications in a single transmission over the interface, either explicitly or using a TCP/IP Nagle algorithm. The format of notifications is shown in Figure A-1.

Notification format

NOTE

All data is specified in network order, which is big-endian.

Reference from: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/BinaryProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH13-SW1

If you find this article is helpful please do not forget to share, recommend and clap.

--

--

Divesh Singh

Postgraduate from JEC Jabalpur || iOS and macOS developer, traveler |||| EM at Darwinbox | Ex-Samsung || Ex - Optimize IT Sys | Ex - R Systems