Article

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 performed, and includes any custom data your app needs to respond to the notification.

Notifications can result in many different kinds of user interactions, including displaying an alert, playing a sound, or badging the app's icon. Notifications can also be configured with no user interactions in some cases.

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:

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

  • For all other remote notifications, the maximum payload size is 4 KB (4096 bytes).

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 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 placed 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. The category key identifies the notification’s type, and is used to add action buttons 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.

Listing 2

A remote notification payload for playing a sound

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

For more information about creating sounds for your notifications, see UNNotificationSound.

Localize Your Alert Messages

There are two ways to localize the content of remote notifications:

  • Include localized strings directly in the payload.

  • Add localized message strings in your app bundle, and let the system choose which strings to display.

Placing localized strings directly into the payload gives you more flexibility, but requires you to track the user’s preferred language on your provider server. Because your provider server supplies the strings, it must know which language to use. On the user's device, you can retrieve the user's preferred languages by examining the preferredLanguages property of NSLocale. Your app can then forward that information to your server.

If the text of your notification messages is predetermined, you can store your message strings in the Localizable.strings file of your app bundle and use the title-loc-key, subtitle-loc-key, and loc-key payload keys to specify which strings you want to display. Your localized strings may contain placeholders so that you can insert content dynamically into the final string. Listing 3 shows an example of a payload with a message derived from an app-provided string. The loc-args key contains an array of replacement variables to substitute into the string.

Listing 3

A remote notification payload with localized content

{
   "aps" : {
      "alert" : {
         "loc-key" : "GAME_PLAY_REQUEST_FORMAT",
         "loc-args" : [ "Shelly", "Rick"]
      }
   }
}

For more information about the keys you use for localized content, see Table 2.

Payload Key Reference

Table 1 lists the keys that you may include in the aps dictionary. To interact with the user, include the alert, badge, or sound keys. Don't add your own custom keys to the aps dictionary; APNs ignores custom keys. Instead, add your custom keys as peers of the aps dictionary, as shown in Listing 1.

Table 1

Keys to include in the aps dictionary

Key

Value type

Description

alert

Dictionary (or String)

The information for displaying an alert. A dictionary is recommended. If you specify a string, the alert displays your string as the body text. For a list of dictionary keys, see Table 2.

badge

Number

The number to display in a badge on your app’s icon. Specify 0 to remove the current badge, if any.

sound

String

The name of a sound file in your app’s main bundle or in the Library/Sounds folder of your app’s container directory. Specify the string "default" to play the system sound. Use this key for regular notifications. For critical alerts, use the sound dictionary instead. For information about how to prepare sounds, see UNNotificationSound.

sound

Dictionary

A dictionary that contains sound information for critical alerts. For regular notifications, use the sound string instead.

thread-id

String

An app-specific identifier for grouping related notifications. This value corresponds to the threadIdentifier property in the UNNotificationContent object.

category

String

The notification’s type. This string must correspond to the identifier of one of the UNNotificationCategory objects you register at launch time. See Declaring Your Actionable Notification Types.

content-available

Number

The background notification flag. To perform a silent background update, specify the value 1 and don't include the alert, badge, or sound keys in your payload. See Pushing Updates to Your App Silently.

mutable-content

Number

The notification service app extension flag. If the value is 1, the system passes the notification to your notification service app extension before delivery. Use your extension to modify the notification’s content. See Modifying Content in Newly Delivered Notifications.

Table 2 lists the keys that you may include in the alert dictionary. Use these strings to specify the title and message to include in the alert banner.

Table 2

Keys to include in the alert dictionary

Key

Value type

Description

title

String

The title of the notification. Apple Watch displays this string in the short look notification interface. Specify a string that is quickly understood by the user.

subtitle

String

Additional information that explains the purpose of the notification.

body

String

The content of the alert message.

launch-image

String

The name of the launch image file to display. If the user chooses to launch your app, the contents of the specified image or storyboard file are displayed instead of your app's normal launch image.

title-loc-key

String

The key for a localized title string. Specify this key instead of the title key to retrieve the title from your app’s Localizable.strings files. The value must contain the name of a key in your strings file.

title-loc-args

Array of strings

An array of strings containing replacement values for variables in your title string. Each %@ character in the string specified by the title-loc-key is replaced by a value from this array. The first item in the array replaces the first instance of the %@ character in the string, the second item replaces the second instance, and so on.

subtitle-loc-key

String

The key for a localized subtitle string. Use this key, instead of the subtitle key, to retrieve the subtitle from your app's Localizable.strings file. The value must contain the name of a key in your strings file.

subtitle-loc-args

Array of strings

An array of strings containing replacement values for variables in your title string. Each %@ character in the string specified by subtitle-loc-key is replaced by a value from this array. The first item in the array replaces the first instance of the %@ character in the string, the second item replaces the second instance, and so on.

loc-key

String

The key for a localized message string. Use this key, instead of the body key, to retrieve the message text from your app's Localizable.strings file. The value must contain the name of a key in your strings file.

loc-args

Array of strings

An array of strings containing replacement values for variables in your message text. Each %@ character in the string specified by loc-key is replaced by a value from this array. The first item in the array replaces the first instance of the %@ character in the string, the second item replaces the second instance, and so on.

Table 3 lists the keys that you may include in the sound dictionary. Use these keys to configure the sound for a critical alert.

Table 3

Keys to include in the sound dictionary

Key

Value Type

Description

critical

Number

The critical alert flag. Set to 1 to enable the critical alert.

name

String

The name of a sound file in your app’s main bundle or in the Library/Sounds folder of your app’s container directory. Specify the string "default" to play the system sound. For information about how to prepare sounds, see UNNotificationSound.

volume

Number

The volume for the critical alert’s sound. Set this to a value between 0.0 (silent) and 1.0 (full volume).

Figure 2 shows the default placement of the title, subtitle, and body content in a banner notification. To customize the appearance of alerts, use a notification content app extension as described in Customizing the Appearance of Notifications.

Figure 2

The default appearance of the notification content

The default presentation for the title, subtitle, and body fields of a notification.

See Also

Server Tasks

Sending Notification Requests to APNs

Send your remote notification payload and device token information to APNs.

Pushing Updates to Your App Silently

Deliver silent notifications and wake up your app in the background on the user's device.