Search code examples
push-notificationgoogle-cloud-messagingpubnub

Not receiving GCM Notifications on Pubnub


I am trying to get Push Notification w/GCM in my PubNub app:

Here is what I am doing :

  1. I registered to regular PUbnub channels and am able to send receive regular messages.
  2. Subscribe to PushNotification Channel :

    var regId = getDeviceRegistrationId(). //This was from GCM register response

    pubnub!!.addPushNotificationsOnChannels()
    .pushType(PNPushType.GCM)
    .channels(Arrays.asList( pubnubChannelID+ CHANNEL_TAG_PUSH))
    .deviceId(regId)
      .async( object  : PNCallback<PNPushAddChannelResult>() {
          override fun onResponse(result: PNPushAddChannelResult?, status: PNStatus?) {
          Timber.d("PNPushAddChannelResult  "+!status!!.isError)  
        }
    });
    
  3. In Pubnub console, I put in a valid GCM api key

  4. I am publishing the Message as below to the Channel registered above :

    { "pn_gcm": { "data": "A Message" }, "pn_apns" : { "aps": { "alert" : { "loc-key" : "MessageAlertKey", "loc-args" : "none"} } } }

The documentation at : https://support.pubnub.com/support/discussions/topics/14000006344#latest is only marginally useful.

What am I missing from receiving a GCM Push Notification on android device with Pubnub?

Appreciate any help.

Thanks


Solution

  • NOTE: the following content can be found in this PubNub Support KB article, but there is a Stack Overflow policy to not reply with a simple link. So I am pasting the content here. I wrote the original, so it's not stealing ;)

    Why are my FCM push notifications not working?

    When you configure and implement FCM into your PubNub application, you may not see the push notifications that you are sending. Before you do any PubNub mobile push troubleshooting, please be sure that you are sending, and receiving, the intended FCM type.

    NOTE: The following comes directly from the Android FCM docs: With FCM, you can send two types of messages to clients:

    • Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
    • Data messages, which are handled by the client app.
    • Notification messages contain a predefined set of user-visible keys and can contain an optional data payload.
    • Data messages, by contrast, contain only your user-defined custom key-value pairs.
    • Maximum payload for both message types is 4KB, except when sending messages from the Firebase console, which enforces a 1024 character limit.

    Following the above, it could be that your code in your app is pulling the wrong message type or you are sending the wrong message type. For example, you could use one, the other or both of these message keys in your FCM (formerly known as, GCM) message payload: data or notification.

    {
        "pn_gcm" : {
            "notification": {
                "title":"Portugal vs. Denmark",
                "body":"great match!"
            },
            "data" : {
                "body" : "great match!",
                "room" : "PortugalVSDenmark"
            }
        }
    }
    

    You only need either data or notification (perhaps both depending on your use case) but it does matter if you only send data because your code is explicitly responsible for displaying the push notification, whereas notification will be automatically displayed by Android.