-->

Pubnub Push Notification not working for cross pla

2019-04-11 07:50发布

问题:

We are developing a cordova based android application which has chat capability. We are sending test chat messages from a server and we received the messages just fine when the app is in foreground.

We would like to get a notification about chat message when app is in background. However we are not getting any Push Notification about chat message.

The steps we followed are mentioned below. Kindly let us know the mistake we're making.

We have referred this link to get notification on device when app is in background.

We use cordova PushPlugin to register device for push notification. And register device using below code

 pubnub.mobile_gw_provision ({
     device_id: 'A655FBA9931AB',
     op: 'add',
     gw_type: 'gcm', // or 'apns'
     channel: 'my_chat',
     callback: mySuccessCallback,
     error: myErrorCallback,
 });

We get the Push Notification if we programmatically send use the GCM device id to send Push notification from our server using GCM API.

We receive notification alert from pubnub in onNotificationGCM method of PushPlugin when application is in foreground.

We have also configured GCM server API key in admin console and enabled pubnub push notification.

I am using below Python code to publish the Chat:

_pubnub = Pubnub(publish_key='our-publish-key',
subscribe_key='our-sub-key')

channel_name = 'here-is-channel-string'
data=dict(
    pn_gcm=dict(data=dict(
        title_for_mobile='PN',
        summary_for_mobile=['Hi, test']
        )
    ),
    text='what is your dob?',
    sender=dict(name='Jon Snow', id='yyyyyyyy'),
    meta=dict(job=dict(id='zzzzzzzz'))
)

_pubnub.publish(channel_name, data)

Getting push notification of incoming chat message when application is in background is a key functionality of my application.

Please let me know what I am doing wrong?

回答1:

I had also raised a support request regarding this on Pubnub's support. After some nice replies from guys at Pubnub, we were able to resolve it.

We were mislead by this documentation, and we were using

"pn_gcm": {
  "data": {
      "title_for_mobile": "Test",
      "summary_for_mobile": [ "Hi" ]
   }
}

Instead, we should have used.

{
  "message": "This is some text",
  "pn_gcm": { 
     "data": {
         "title": "Demo wpush", 
         "message":"This is a pushnotification"
     }
   },
   "pn_debug": "true"
} 

Because Cordova PushPlugin is expecting message key from GCM.

// GCMIntentService.java
@Override
protected void onMessage(Context context, Intent intent) {
    //.... some code ...
    if (extras.getString("message") != null && extras.getString("message").length() != 0) {
          createNotification(context, extras);
    }
    //... some code ...