Push notifications

Mobile Push Notifications feature enables you to easily integrate with third-party services including FCM (Firebase Cloud Messaging) and APNs (Apple Push Notification service) to trigger mobile push notifications.

Responding to a message immediately can make all the difference. If a prospective client is looking for information or a critical service is down, you want to be in the loop 24/7. You can use PubNub to send your users mobile push notifications to keep them up to date when their chat window is closed, they are not actively using your app, or even when they are offline. This way, your users can stay informed about important messages you don't want them to miss.

Registering mobile device tokens

Mobile Push allows you to associate devices (push tokens) with channels. When a message is published to a push-enabled channel, all associated devices receive that message via their respective push service (FCM or APNs).

Note that you must first enable Mobile Push Notifications from the Admin Portal to use them in your app. For more details on working with mobile push notifications, refer to our Push Notification documentation.

Retrieving device tokens

Before you can use either service, you must be registered to use Firebase Cloud Messaging (FCM) or the Apple Push Notification service (APNs).

Each device that runs your app has a unique device token, which you need to register to be able to send mobile push notifications. You can obtain the token from your user's device using either your native app, or Cordova/PhoneGap with the Cordova Push plugin.

To retrieve an iOS device token, follow this Apple guide.

To retrieve an Android registration token, follow this Google guide.

Adding a device token to channels

This method associates a device token with one or more channels.

// FCM
pubnub.push.addChannels(
{
channels: ["chats.room1", "chats.room2", "alerts.system"],
device: deviceToken,
pushGateway: "gcm",
},
function(status) {
console.log(status);
}
);

// APNs2
pubnub.push.addChannels(
{
show all 25 lines

Removing a device token from channels

This method disassociates a device token from one or more channels.

// FCM
pubnub.push.removeChannels({
channels: ['ch-1', 'ch-2'],
device: 'myDeviceId',
pushGateway: 'gcm',
}, (status) => {
if (status.error) {
console.log('operation failed w/ status: ', status);
} else {
console.log('operation done!');
}
});

// APNs2
pubnub.push.removeChannels({
show all 25 lines

Publishing messages with push payloads

If you want to trigger mobile push notifications, include one or both endpoint keys (pn_apns or pn_fcm) in your message before you send it to PubNub. Users connected to PubNub channels receive the message portion of the payload, and each third-party endpoint receives the data encapsulated in its associated endpoint key:

  • APNs devices receive only the data within the pn_apns key
  • FCM devices receive only the data within the pn_fcm key
const messagePayload = {
"pn_apns":{
"aps":{
"alert":{
"title":"Chat invitation",
"body":"John invited you to chat"
}
},
"pn_push":[
{
"push_type":"alert",
"targets":[
{
"environment":"production",
"topic":"BUNDLE_ID_FOR_APP_1"
show all 40 lines
Last updated on