PubNub's Mobile Push Gateway enables you to easily integrate with third-party push notification services including FCM (Firebase Cloud Messaging) and APNS (Apple Push Notification Service).
Ideally, mobile clients would always be connected to the PubNub network to receive messages in real time. However, there are situations where this isn't possible (for example, when an app is closed or running in the background). The Mobile Push Gateway makes it easy to fall back to push notifications when the client isn't connected to PubNub.
When you publish a message containing a specially-formatted payload (a key-value pair) for the appropriate notification service, PubNub recognizes the published message as a push notification payload, and hands the contents to the notification service (APNS or FCM), along with all the device tokens registered to receive push notifications on that channel.
Formatting your message payload for APNS and FCM
If you want to trigger push notifications, include one or both endpoint keys (
pn_gcm) 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
- FCM devices receive only the data within the
Retrieving mobile 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 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
This method associates a device token with one or more channels.
Removing a device token
This method disassociates a device token from one or more channels.
Removing all notifications for a device token
This method unsubscribes a device token from all push notifications.