phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

[Android 5.1] Case study: depending on notif payload, `onNotification` callback not called when notif clicked while app in background #746

Open jakub-g opened 8 years ago

jakub-g commented 8 years ago

Expected Behaviour

The onNotification callback should be fired when user clicks the notif in notification drawer, regardless if the app was not in foreground / background / not running

Actual Behaviour

When the app is in background when user clicks the notif, or the app was started from user clicking the notification, the callback may not get called, depending on the notif payload

Reproduce Scenario (including but not limited to)

  1. Launch the app on Android and make it go to background
  2. Send a notification with node-gcm
  3. Observe the notification is displayed in the notification drawer.
  4. Click the notification
  5. Check the logs in Chrome Dev Tools

    Platform and Version

Android 5.1

(Android) What device vendor

Motorola MotoG3

Cordova CLI version and cordova platform version

cordova --version
> 6.0.0
cordova platform version android
> 4.1.1

Plugin version

cordova plugin version | grep phonegap-plugin-push
> 1.6.0

Sample Code that illustrates the problem

_pushPlugin = window.PushNotification.init({
  android: {
    senderID: GCM_SENDER_ID,
    iconColor: "blue",
    icon: "push_notification_icon",
    // when app is in foreground, we want the callback called immediately, without displaying
    // the notif in notification drawer
    forceShow: false
  }
});

_pushPlugin.on('error', function(err) {
  $log.error('[onError]');
});

_pushPlugin.on('registration', function(data) {
  $log.info('[onRegistration]');
}

_pushPlugin.on('notification', function(data) {
  $log.info('[onNotification]');
}

Sample Push Data Payload

Will post separate cases in the next message

jakub-g commented 8 years ago

Actual behavior

So depending on the payload in node-gcm, the plugin behaves differently.

Sometimes, the phonegap-plugin-push#onNotification is called just fine, but sometimes it is not called.

Accidentally, since I'm also using a webintent plugin, registered as window.plugins.webintent.onNewIntent(__myHandleWebIntentCb);, I've noticed in the cases where phonegap-plugin-push#onNotification is not called, the webintent's callback is called instead, which may shed some light on the underlying issue.

Though I think, the presence of webintent plugin is not the reason of the bug. When I remove the webintent plugin from the app, no callback is still called,

So here are the different cases:

1a) [onNotification] is called

var message = new gcm.Message();
message.addData({
    customField: 'my custom field',
    alert: 'ALERT'
});

1b) [onNotification] is called

var message = new gcm.Message({
    notification: {}
});
message.addData({
    customField: 'my custom field',
    alert: 'ALERT'
});

2) [onNotification] NOT CALLED, instead __myHandleWebIntentCb is called!

var message = new gcm.Message({
    notification: {
        icon: 'push_notification_icon',
        color: 'blue'  
    }
});
message.addData({
    customField: 'my custom field',
    alert: 'ALERT'
});

3) [onNotification] NOT CALLED, instead __myHandleWebIntentCb is called!

var message = new gcm.Message({
    notification: {
        title: "TITLE",
        body: "BODY"
    }
});
message.addData({
    customField: 'my custom field',
    alert: 'ALERT'
});

And here's how different cases look in the UI:

notifs_depending_on_gcm_payload

jakub-g commented 8 years ago

I originally tested with 1.5.3 but the behavior is identical in 1.6.0

jakub-g commented 8 years ago

You can also see in one of the cases the icon is not displayed, I opened #747 for that

macdonst commented 8 years ago

@jakub-g Yeah, the problem is when you add the notification property to the gcm.Message object with a title and body the OS takes over. Once the OS takes over the plugin never gets called. It's on my list to look into but for now I suggest not populating the notification object. That's why all of my sample node-gcm scripts use the data object only.

VivaBem commented 8 years ago

Hello, i noticed when i avoid to set notification (as said before), the phone receives the push, but it comes empty (no title, no body, no sound) on the notification drawer. The title, body and sound are defined on the data object.

The strange behavior is that it works for the first message that arrives on the message bar (it comes with title, body and sound), but the others come empty.

Any idea how to solve it?