Mobile Push Notifications API for JavaScript SDK

The Mobile Push Notifications API of the PubNub SDK for JavaScript registers a device token on channels. PubNub forwards a message with a push payload to each device registered on its channel, through Google Android FCM (Firebase Cloud Messaging) or 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​

addChannels() enables mobile push notifications for a device on a set of channels with a PubNub client for JavaScript.

required
channels required#
Type: string[]
Default: no default
Channels on which to enable mobile push notifications for the device. Pass at least one channel. An empty list fails validation with the message Missing Channels.
device required#
Type: string
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For apns2, the token is a hexadecimal string, and the service rejects any other format with a 400 Invalid device token error. For fcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Missing Device ID (device).
pushGateway required#
Type: string
Default: no default
Push service that delivers the notifications to the device. Accepted values: apns2, fcm. A missing value fails validation with the message Missing GW Type (pushGateway: fcm or apns2).
environment#
Type: string
Default: development
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: development, production. The SDK applies the default only when pushGateway is apns2.
topic#
Type: string
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with the apns2 push gateway and no topic fails validation with the message Missing APNS2 topic.

Returns​

addChannels() returns an empty object when the request succeeds.If the request fails, the promise rejects with a PubNubError, and its status property describes the failure. With a callback, the callback receives this status on success.

Syntax

1pubnub.push.addChannels({
2 channels: string[],
3 device: string,
4 pushGateway: 'apns2' | 'fcm',
5 environment?: 'development' | 'production',
6 topic?: string
7}): Promise<Record<string, unknown>>;

List push notifications channels for a device​

listChannels() returns the channels on which a device receives mobile push notifications, using a PubNub client for JavaScript.

required
device required#
Type: string
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For apns2, the token is a hexadecimal string, and the service rejects any other format with a 400 Invalid device token error. For fcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Missing Device ID (device).
pushGateway required#
Type: string
Default: no default
Push service that delivers the notifications to the device. Accepted values: apns2, fcm. A missing value fails validation with the message Missing GW Type (pushGateway: fcm or apns2).
environment#
Type: string
Default: development
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: development, production. The SDK applies the default only when pushGateway is apns2.
topic#
Type: string
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with the apns2 push gateway and no topic fails validation with the message Missing APNS2 topic.
start#
Type: string
Default: no default
The channel at which the next page of results starts. Pass the last channel from the previous page.
count#
Type: number
Default: 500
The number of channels to return. The SDK replaces a value above 1000 with 1000.

Returns​

listChannels() returns an object that holds the channels when the request succeeds.If the request fails, the promise rejects with a PubNubError, and its status property describes the failure. With a callback, the callback receives the status and the response.

Syntax

1pubnub.push.listChannels({
2 device: string,
3 pushGateway: 'apns2' | 'fcm',
4 environment?: 'development' | 'production',
5 topic?: string,
6 start?: string,
7 count?: number
8}): Promise<{ channels: string[] }>;

Remove a device from push notifications channels​

removeChannels() disables mobile push notifications for a device on the selected channels with a PubNub client for JavaScript.

required
channels required#
Type: string[]
Default: no default
Channels on which to disable mobile push notifications for the device. Pass at least one channel. An empty list fails validation with the message Missing Channels.
device required#
Type: string
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For apns2, the token is a hexadecimal string, and the service rejects any other format with a 400 Invalid device token error. For fcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Missing Device ID (device).
pushGateway required#
Type: string
Default: no default
Push service that delivers the notifications to the device. Accepted values: apns2, fcm. A missing value fails validation with the message Missing GW Type (pushGateway: fcm or apns2).
environment#
Type: string
Default: development
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: development, production. The SDK applies the default only when pushGateway is apns2.
topic#
Type: string
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with the apns2 push gateway and no topic fails validation with the message Missing APNS2 topic.

Returns​

removeChannels() returns an empty object when the request succeeds.If the request fails, the promise rejects with a PubNubError, and its status property describes the failure. With a callback, the callback receives this status on success.

Syntax

1pubnub.push.removeChannels({
2 channels: string[],
3 device: string,
4 pushGateway: 'apns2' | 'fcm',
5 environment?: 'development' | 'production',
6 topic?: string
7}): Promise<Record<string, unknown>>;

Remove a device from all push notifications channels​

deleteDevice() disables mobile push notifications for a device on all channels registered for its device token, using a PubNub client for JavaScript.

required
device required#
Type: string
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For apns2, the token is a hexadecimal string, and the service rejects any other format with a 400 Invalid device token error. For fcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Missing Device ID (device).
pushGateway required#
Type: string
Default: no default
Push service that delivers the notifications to the device. Accepted values: apns2, fcm. A missing value fails validation with the message Missing GW Type (pushGateway: fcm or apns2).
environment#
Type: string
Default: development
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: development, production. The SDK applies the default only when pushGateway is apns2.
topic#
Type: string
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with the apns2 push gateway and no topic fails validation with the message Missing APNS2 topic.

Returns​

deleteDevice() returns an empty object when the request succeeds.If the request fails, the promise rejects with a PubNubError, and its status property describes the failure. With a callback, the callback receives this status on success.

Syntax

1pubnub.push.deleteDevice({
2 device: string,
3 pushGateway: 'apns2' | 'fcm',
4 environment?: 'development' | 'production',
5 topic?: string
6}): Promise<Record<string, unknown>>;

Push notification format configuration​

PubNub.notificationPayload() builds the APNs and FCM payloads for messages that you publish with a PubNub client for JavaScript to trigger mobile push notifications.

Pass the object that buildPayload() returns as the message of publish(). The payload types on this page configure the APNs and FCM parts of that object.

Cross-platform notifications payload​

NotificationsPayload holds the title, the body, and the settings that APNs and FCM share. PubNub.notificationPayload() creates it, and its apns and fcm properties give access to the platform-specific builders.

required
title required#
Type: string
Default: no default
Title of the notification.
body required#
Type: string
Default: no default
Body text of the notification, shown under the title.
required
platforms required#
Type: Array<string>
Default: no default
Platforms for which the payload is built. Accepted values: apns, apns2, fcm. If the list has both apns and apns2, the builder uses apns. The apns2 platform requires at least one entry in apns.configurations, otherwise buildPayload() throws a ReferenceError with the message APNS2 configuration is missing.

Properties​

subtitle#
Type: string
Default: no default
Second line of the notification. Only APNs uses it, and the FCM payload ignores it.
badge#
Type: number
Default: no default
Badge count of the app icon. The builder writes it to aps.badge for APNs and to android.notification.notification_count for FCM.
sound#
Type: string
Default: no default
Name of the sound file in the app resource bundle that plays when the notification arrives.
debugging#
Type: boolean
Default: false
When true, buildPayload() adds pn_debug: true to the payload, so PubNub publishes debug information about the push delivery.
apns#
Default: no default
Builder for the options that only APNs uses.
fcm#
Default: no default
Builder for the options that only FCM uses.

Returns​

PubNub.notificationPayload() returns a NotificationsPayload instance.

buildPayload() returns an object that you can publish as a message to trigger remote notifications. The object has a pn_apns key for APNs and a pn_fcm key for FCM. A key is missing when its platform produced no content, for example an APNs payload with no title, body, or silent flag. The object has pn_debug: true only when debugging is true.

The custom configuration sets collapseId to invitations and expirationDate to ten seconds after the code runs. APNs tries to deliver the notification to the device until that date. Notifications with the invitations collapse identifier appear as one entry in the notification center.

APNSNotificationPayload​

APNSNotificationPayload holds the options that only APNs uses. Access it through the apns property of NotificationsPayload.

Properties​

configurations#
Type: APNS2Configuration[]
Default: no default
HTTP/2 APNs delivery configurations, as APNS2Configuration objects. Write-only. The apns2 platform requires at least one entry. The setter ignores an empty list.
notification#
Type: object
Default: no default
Read-only aps object of the payload. The aps object holds alert, and badge and sound when set.
payload#
Type: object
Default: no default
Read-only payload object. The builder copies any custom top-level keys that you add to it into pn_apns.
silent#
Type: boolean
Default: false
Write-only. When true, the builder sets content-available to 1 and omits alert, sound, and badge.

APNS2Configuration​

APNS2Configuration is the type of one HTTP/2 APNs delivery configuration. Assign an array of these objects to configurations in APNSNotificationPayload.

required
collapseId#
Type: string
Default: no default
Collapse identifier that groups notifications. PubNub sends it to APNs as the apns-collapse-id header.
expirationDate#
Type: Date
Default: no default
Date until which APNs tries to deliver the notification. The builder writes it as an ISO 8601 string, and PubNub sends it to APNs as the apns-expiration header.
targets required#
Type: APNS2Target[]
Default: no default
Delivery targets, as APNS2Target objects. Pass at least one target. An empty list makes buildPayload() throw a ReferenceError with the message At least one APNS2 target should be provided.

APNS2Target​

APNS2Target is the type of one delivery target of an HTTP/2 APNs configuration.

required
topic required#
Type: string
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. An empty topic makes buildPayload() throw a TypeError with the message Target 'topic' undefined.
environment#
Type: string
Default: development
APNs environment of the target devices. Accepted values: development, production.
excludedDevices#
Type: string[]
Default: []
Push tokens of the devices that must not receive the notification. The builder writes excluded_devices only when the list is not empty.

FCMNotificationPayload​

FCMNotificationPayload holds the options that only FCM uses. Access it through the fcm property of NotificationsPayload.

Properties​

notification#
Type: object
Default: no default
Read-only notification object of the payload. The notification object holds title and body.
data#
Type: object
Default: {}
Read-only object with custom key-value data. All values must be strings. Do not use reserved FCM keys, such as from, message_type, or keys that start with google or gcm.
silent#
Type: boolean
Default: false
Write-only. When true, the builder sends a data-only message. The builder moves the title, the body, and the Android fields sound, icon, tag, and notification_count into data as strings and omits notification.
icon#
Type: string
Default: no default
Name of the icon file in the app resource bundle. The builder writes it to android.notification.icon, so only Android devices use it.
tag#
Type: string
Default: no default
Tag that groups similar notifications. The builder writes it to android.notification.tag, so only Android devices use it.
payload#
Type: object
Default: no default
Read-only payload object. The builder copies custom top-level FCM fields that you add to it, such as apns, webpush, or pn_exceptions, into pn_fcm.

Cross-platform notifications payload - Syntax

1PubNub.notificationPayload(
2 title: string,
3 body: string
4): NotificationsPayload;

Was this page useful?

Last updated on