The community forums are in BETA and closed to the public.

Sending APNS and GCM Messages to Subscribers and Mobile Push Notification Services in One API Call

Broadcasting APNs, MPNs and GCM Messages easily in One API Call

Many are often confused about the difference between a PubNub realtime messages and mobile push notifications.

How is subscribing for realtime messages different from registering for push notifications?

  • Subscribe to a channel(s) for realtime messages using the specific subscribe API in each SDK.
  • Register the device to channel(s) for push notifications: we have an API for Objective-C, Android, JavaScript (phonegap) and REST.
  • The channel for realtime messages delivery can be, and often is, the same as the channel used for mobile push notifications.

How is publishing realtime messages different from sending mobile push notifications?

  • There is no difference - just use the publish API for the SDK you are using.

What data is sent to the realtime channel vs. what is sent to the push notification services?

  • Data can be provided in a single message payload that targets different gateways.
  • Provide the content in the message payload that is specific for each gateway: realtime, APNS & GCM (see sample payload format below).

When does the mobile device receive realtime messages vs. mobile push notifications?

  • When a mobile app is not active (suspended in the background or not running at all) then the device will receive only mobile push notifications that the device is registered to receive.
  • When a mobile app is active (running in the foreground) then a message that is published on a channel that the app is both subscribed to for realtime messages and registered to for mobile push notifications, then it will receive both messages. Simply use the mobile OS's API to receive the mobile push notification and ignore it (don't display it). For example, on iOS the API is didReceiveRemoteNotification (Android has a similar API).

How do you format the message payload to target realtime message delivery as well as mobile push notification delivery with one publish?

Dictionary objects sent to APNS and GCM mobile services must be in a well-formed format in order for the remote service (operated by Apple, Google, etc) to process.

For example, when sending native APNS and GCM alerts to Apple and Google via PubNub, you could send in this format:

{
    "pn_apns": {
        "aps": {
            "alert": "Your order is ready for pickup!",
            "badge": 1,
            "payment_info": {
                "credit_card": 987656789876,
                "expiration": "0108"
            }
        }
    },
    "pn_gcm": {
        "data": "this is my data only for gcm devices"
    },
    "data_for_all": {
        "info": "This is data all non-APNS and non-GCM devices would receive. They would also receive the pn_apns and pn_gcm data."
    }
}

When the above message is published:

  • APNS gateway enabled devices will only receive the data contained from within the pn_apns key (up to 2K characters).
  • GCM gateway enabled devices will only receive the data contained from within the pn_gcm key (up to 4K characters).
  • Non-APNS/GCM devices will receive the entire message payload, including the push gateway keys/values (up to 32K character including the APNS and GCM data).

As noted above, the value of the APNS object (contained within the pn_apns key) contains an object that will be forwarded in whole to the APNS gateway.

Note that it has one mandatory key: aps. Within the aps object, it has other non-mandatory fields, which include alert and badge, as well as some additional user-info/metadata fields contained within the payment_info key: credit_card and expiration.

See also


1 person has this question

In regards to:

"When a mobile app is not active (suspended in the background or not running at all) then the device will receive only mobile push notifications that the device is registered to receive."

How do I tell pubnub the app is suspended in the background? Does this mean I have called unsubscribe on my channel but not called removeAllPushNotificationsForDeviceRegistrationId?

You do not tell PubNub that the app is suspended. The connection will be gone when the app goes to background (unless you have configured to run in background) and the realtime message will not be sent to that that subscriber. In other words, there is nothing more for you to do.


Comments to this discussion are now closed!