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
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
Requires Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
addChannels() enables mobile push notifications for a device on a set of channels with a PubNub client for JavaScript.
channelsrequired#- 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. devicerequired#- 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 a400Invalid device tokenerror. Forfcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageMissing Device ID (device). pushGatewayrequired#- Push service that delivers the notifications to the device. Accepted values:
apns2,fcm. A missing value fails validation with the messageMissing GW Type (pushGateway: fcm or apns2). environment#- 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 whenpushGatewayisapns2. topic#- 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.
Sample code
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.Examples for Add a device to a push notifications channel
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>>;
Add device to channel
Add device to channel
1import PubNub from 'pubnub';
2// Initialize PubNub with your keys
3const pubnub = new PubNub({
4 publishKey: 'YOUR_PUBLISH_KEY',
5 subscribeKey: 'YOUR_SUBSCRIBE_KEY',
6 userId: 'YOUR_USER_ID',
7});
1// Function to add a device to a channel for APNs2
2const response = await pubnub.push.addChannels({
3 channels: ['a', 'b'],
4 device: '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad',
5 pushGateway: 'apns2',
6 environment: 'production',
7 topic: 'com.example.bundle_id',
8});
9console.log('device added to channels response:', response);
10
11// Function to add a device to a channel for FCM
12const response2 = await pubnub.push.addChannels({
13 channels: ['a', 'b'],
14 device: '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad',
15 pushGateway: 'fcm',
show all 17 linesReturns
1{
2 error: false,
3 operation: 'PNAddPushNotificationEnabledChannelsOperation',
4 category: 'PNAcknowledgmentCategory',
5 statusCode: 200
6}
List push notifications channels for a device
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
Requires Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
listChannels() returns the channels on which a device receives mobile push notifications, using a PubNub client for JavaScript.
devicerequired#- 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 a400Invalid device tokenerror. Forfcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageMissing Device ID (device). pushGatewayrequired#- Push service that delivers the notifications to the device. Accepted values:
apns2,fcm. A missing value fails validation with the messageMissing GW Type (pushGateway: fcm or apns2). environment#- 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 whenpushGatewayisapns2. topic#- 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#- The channel at which the next page of results starts. Pass the last channel from the previous page.
count#- The number of channels to return. The SDK replaces a value above
1000with1000.
Sample code
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.Examples for List push notifications channels for a device
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[] }>;
List channels for device
List channels for device
1// for APNs2
2const response = await pubnub.push.listChannels({
3 device: '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad',
4 pushGateway: 'apns2',
5 environment: 'production',
6 topic: 'com.example.bundle_id',
7});
8console.log('listing channels for device response:', response);
9response.channels.forEach((channel) => {
10 console.log(channel);
11});
12
13// for FCM
14const response2 = await pubnub.push.listChannels({
15 device: '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad',
show all 23 linesReturns
1// Example of status
2{
3 error: false,
4 operation: 'PNPushNotificationEnabledChannelsOperation',
5 category: 'PNAcknowledgmentCategory',
6 statusCode: 200
7}
8
9// Example of response
10{
11 channels: [ 'a', 'b' ]
12}
Remove a device from push notifications channels
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
Requires Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
removeChannels() disables mobile push notifications for a device on the selected channels with a PubNub client for JavaScript.
channelsrequired#- 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. devicerequired#- 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 a400Invalid device tokenerror. Forfcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageMissing Device ID (device). pushGatewayrequired#- Push service that delivers the notifications to the device. Accepted values:
apns2,fcm. A missing value fails validation with the messageMissing GW Type (pushGateway: fcm or apns2). environment#- 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 whenpushGatewayisapns2. topic#- 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.
Sample code
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.Examples for Remove a device from push notifications channels
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 device from channel
Remove device from channel
1// for APNs2
2const response = await pubnub.push.removeChannels({
3 channels: ['a', 'b'],
4 device: '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad',
5 pushGateway: 'apns2',
6 environment: 'production',
7 topic: 'com.example.bundle_id',
8});
9
10console.log('removing device from channel response:', response);
11
12// for FCM
13const response2 = await pubnub.push.removeChannels({
14 channels: ['a', 'b'],
15 device: '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad',
show all 19 linesReturns
1{
2 error: false,
3 operation: 'PNRemovePushNotificationEnabledChannelsOperation',
4 category: 'PNAcknowledgmentCategory',
5 statusCode: 200
6}
Remove a device from all push notifications channels
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
Requires Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
deleteDevice() disables mobile push notifications for a device on all channels registered for its device token, using a PubNub client for JavaScript.
devicerequired#- 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 a400Invalid device tokenerror. Forfcm, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageMissing Device ID (device). pushGatewayrequired#- Push service that delivers the notifications to the device. Accepted values:
apns2,fcm. A missing value fails validation with the messageMissing GW Type (pushGateway: fcm or apns2). environment#- 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 whenpushGatewayisapns2. topic#- 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.