Mobile Push Notifications API for JavaScript SDK
The Mobile Push Notifications feature connects native PubNub publishing to third-party push services. Supported services include Google Android FCM (Firebase Cloud Messaging) and Apple iOS APNs (Apple Push Notification service).
To learn more, read about Mobile Push Notifications.
Supported and recommended asynchronous patterns
PubNub supports Callbacks, Promises, and Async/Await for asynchronous JS operations. The recommended pattern is Async/Await and all sample requests in this document are based on it. This pattern returns a status only on detecting an error. To receive the error status, you must add the try...catch syntax to your code.
Add a device to a push notifications channel
Requires Mobile Push Notifications add-on
Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.
Enable mobile push notifications on a set of channels.
Method(s)
1pubnub.push.addChannels({
2 channels: Array<string>,
3 device: string,
4 pushGateway: string,
5 environment: string,
6 topic: string
7})
| Parameter | Description |
|---|---|
channels *Type: Array <string>Default: n/a | Channels to enable for push notifications. |
device *Type: string Default: n/a | Device token. |
pushGateway *Type: string Default: n/a | Accepted values: apns2, gcm. |
environmentType: string Default: development | APNs environment. Accepted values: development, production. Required if pushGateway is apns2. |
topicType: string Default: n/a | APNs topic (bundle identifier). Required if pushGateway is apns2. |
Sample code
Reference code
Add device to channel
1
1
Response
1{
2 error: false,
3 operation: 'PNPushNotificationEnabledChannelsOperation',
4 statusCode: 200
5}
List push notifications channels for a device
Requires Mobile Push Notifications add-on
Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.
Get all channels with push notifications for the specified push token.
Method(s)
1pubnub.push.listChannels({
2 device: string,
3 pushGateway: string,
4 environment: string,
5 topic: string
6})
| Parameter | Description |
|---|---|
device *Type: string Default: n/a | Device token. |
pushGateway *Type: string Default: n/a | Accepted values: apns2, gcm. |
environmentType: string Default: development | APNs environment. Accepted values: development, production. Required if pushGateway is apns2. |
topicType: string Default: n/a | APNs topic (bundle identifier). Required if pushGateway is apns2. |
startType: string Default: n/a | Start channel for pagination. Use the last channel from the previous page. |
countType: number Default: n/a | Number of channels to return. Maximum 1000; default 500. |
Sample code
List channels for device
1
Response
1// Example of status
2{
3 error: false,
4 operation: 'PNPushNotificationEnabledChannelsOperation',
5 statusCode: 200
6}
7
8// Example of response
9{
10 channels: [ 'a', 'b' ]
11}
Remove a device from push notifications channels
Requires Mobile Push Notifications add-on
Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.
Disable push notifications on selected channels.
Method(s)
1pubnub.push.removeChannels({
2 channels: Array<string>,
3 device: string,
4 pushGateway: string,
5 environment: string,
6 topic: string
7})
| Parameter | Description |
|---|---|
channels *Type: Array <string>Default: n/a | Channels to disable for push notifications. |
device *Type: string Default: n/a | Device token. |
pushGateway *Type: string Default: n/a | Accepted values: apns2, gcm. |
environmentType: string Default: development | APNs environment. Accepted values: development, production. Required if pushGateway is apns2. |
topicType: string Default: n/a | APNs topic (bundle identifier). Required if pushGateway is apns2. |
Sample code
Remove device from channel
1
Response
1{
2 error: false,
3 operation: 'PNPushNotificationEnabledChannelsOperation',
4 statusCode: 200
5}
Remove a device from all push notifications channels
Requires Mobile Push Notifications add-on
Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.
Disable push notifications from all channels registered for the specified push token.
Method(s)
1pubnub.push.deleteDevice({
2 device: string,
3 pushGateway: string,
4 environment: string,
5 topic: string
6})
| Parameter | Description |
|---|---|
device *Type: string Default: n/a | Device token. |
pushGateway *Type: string Default: n/a | Accepted values: apns2, gcm. |
environmentType: string Default: development | APNs environment. Accepted values: development, production. Required if pushGateway is apns2. |
topicType: string Default: n/a | APNs topic (bundle identifier). Required if pushGateway is apns2. |
Sample code
Remove all mobile push notifications
1
Response
1{
2 error: false,
3 operation: 'PNPushNotificationEnabledChannelsOperation',
4 statusCode: 200
5}
Push notification format configuration
Push notifications enforce specific message format requirements. Use these methods to configure your APNs and FCM mobile push notifications.
APNS2Configuration
APNS2 configuration type.
Method(s)
1type APNS2Configuration = {
2 collapseId?: string,
3 expirationDate?: Date,
4 targets: Array<APNS2Target>
5}
| Parameter | Description |
|---|---|
collapseIdType: string | Collapse identifier (apns-collapse-id). |
expirationDateType: Date | Expiration (apns-expiration). |
targets *Type: Array< APNS2Target> | Delivery targets. |
APNSNotificationPayload
APNSNotificationPayload provides APNs-only options.
Properties
| Parameter | Description |
|---|---|
configurationsType: Array< APNSNotificationConfiguration> | HTTP/2 APNs delivery configurations. |
notificationType: Hash | User-visible key-value pairs. |
payloadType: Hash | Platform-specific payload for additional data. |
silentType: Boolean | If true, omits alert, sound, and badge. |
APNS2Target
APNS2 configuration target type.
Method(s)
1type APNS2Target = {
2 topic: string,
3 environment?: 'development' | 'production',
4 excludedDevices?: Array<string>
5}
| Parameter | Description |
|---|---|
topic *Type: string Default: n/a | APNs topic (bundle identifier). |
environmentType: string Default: development | Accepted values: development, production. |
excludedDevices *Type: Array Default: n/a | Push tokens to exclude. |
FCMNotificationPayload
FCMNotificationPayload provides FCM-only options.
Properties
| Parameter | Description |
|---|---|
notificationType: Hash | User-visible key-value pairs. |
dataType: Hash | Additional key-value data (stringify values). Must not include reserved keys (for example, from, message_type, keys starting with google or gcm). See the Firebase table. |
silentType: Boolean | If true, moves notification under data. |
iconType: String | Icon shown with the notification title. |
tagType: String | Identifier used to update/replace a prior notification. |
payloadType: Hash | Platform-specific payload for additional data. |
Cross-platform notifications payload
NotificationsPayload helps build multi-platform payloads and access platform-specific builders.
Method(s)
| Parameter | Description |
|---|---|
subtitleType: string | Additional context for the notification. |
badgeType: number | Badge count for supported platforms. |
soundType: string | Sound name or file path to play. |
debuggingType: boolean | Include device delivery debug info. |
apnsType: APNSNotificationPayload | APNs-specific builder. |
fcmType: FCMNotificationPayload | FCM-specific builder. |
1PubNub.notificationPayload(
2 title: string,
3 body: string
4)
| Parameter | Description |
|---|---|
titleType: string | Title shown in the notification. |
bodyType: string | Body text shown under the title. |
1buildPayload(
2 platforms: Array<string>
3)
| Parameter | Description |
|---|---|
platforms *Type: Array <string> | List of platforms for which payload should be added to final dictionary. Available:
|
Sample code
Create notification payload builder with pre-defined notification title and body:
1
1let builder = PubNub.notificationPayload('Chat invitation',
2 'You have been invited to \'quiz\' chat');
Response
Hash with data, which can be sent with publish method call and trigger remote notifications for specified platforms.
Other examples
Generate simple notification payload for FCM and APNS
1
Generate simple notification payload for FCM and HTTP/2-based APNs (default configuration)
1
Generate simple notification payload for FCM and HTTP/2-based APNs (custom configuration)
1
Output
1{
2 "pn_apns": {
3 "aps": {
4 "alert": {
5 "body": "Chat invitation",
6 "title": "You have been invited to 'quiz' chat"
7 }
8 },
9 "pn_push": [
10 {
11 "collapse_id": "invitations",
12 "expiration": "2019-11-28T22:06:09.163Z",
13 "targets": [
14 {
15 "environment": "development",
show all 29 linesExample above show how to create notification payload which APNS will try to redeliver few times (if devices not active) and give up after 10 seconds since moment when it has been scheduled.
Additionally this invitation notification will be grouped along with other invitation notifications (using provided collapse_id as group identifier) and shown as one in notification center.
Returns
Configured and ready to use NotificationsPayload instance.