phonegap / phonegap-plugin-push

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

The app doesn't goes from background to foreground when a notification arrives and is clicked/tapped. #880

Closed anicarrr closed 8 years ago

anicarrr commented 8 years ago

Expected Behaviour

With the app in background when a notification arrives the "onNotification" event is triggered and does some logic and then when I click the main body of the notification the app goes to foreground.

Actual Behaviour

The notification arrives and the "onNotification" event is triggered and some logic is executed but when I click in the main body of the notification instead of the app going to foreground, the "onNotification" event triggers again and then the app stays in background.

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 5.1

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Motorola X.

Cordova CLI version and cordova platform version

cordova --version                                    # e.g. 5.4.1
cordova platform version android                     # e.g. 4.1.1

Plugin version

cordova plugin version | grep phonegap-plugin-push   # e.g. 1.6.3
macdonst commented 8 years ago

@anicarrr what does your AndroidManifest.xml look like? Specifically, what is your android:launchMode?

anicarrr commented 8 years ago

@macdonst

android:launchMode="singleTop"

macdonst commented 8 years ago

@anicarrr that looks correct. Can you reproduce the problem while running adb logcat | grep PushPlugin?

anicarrr commented 8 years ago

@macdonst When I receive the notification some logic is executed because of the "content-available" flag, but when I click the main body of the notication the logic is executed again and the app stay in background, I want the app to go to foreground without executing the logic again!

This is the log:

D/PushPlugin_GCMIntentService(31008): onMessage - from: 668220855123 D/PushPlugin_GCMIntentService(31008): normalize extras D/PushPlugin_GCMIntentService(31008): key = content-available D/PushPlugin_GCMIntentService(31008): replace key content-available with content-available D/PushPlugin_GCMIntentService(31008): key = payload D/PushPlugin_GCMIntentService(31008): replace key payload with payload D/PushPlugin_GCMIntentService(31008): key = icon D/PushPlugin_GCMIntentService(31008): replace key icon with icon D/PushPlugin_GCMIntentService(31008): key = image D/PushPlugin_GCMIntentService(31008): replace key image with image D/PushPlugin_GCMIntentService(31008): key = title D/PushPlugin_GCMIntentService(31008): replace key title with title D/PushPlugin_GCMIntentService(31008): key = vibrationPattern D/PushPlugin_GCMIntentService(31008): replace key vibrationPattern with vibrationPattern D/PushPlugin_GCMIntentService(31008): key = message D/PushPlugin_GCMIntentService(31008): replace key message with message D/PushPlugin_GCMIntentService(31008): key = collapse_key D/PushPlugin_GCMIntentService(31008): replace key collapse_key with collapse_key D/PushPlugin_GCMIntentService(31008): background D/PushPlugin_GCMIntentService(31008): message =[SOLM required by watch center] D/PushPlugin_GCMIntentService(31008): title =[SOLM REQUEST] D/PushPlugin_GCMIntentService(31008): contentAvailable =[1] D/PushPlugin_GCMIntentService(31008): create notification E/PushPlugin_GCMIntentService(31008): Number format exception - Error parsing notId: Invalid int: "null" D/PushPlugin_GCMIntentService(31008): stored icon=null D/PushPlugin_GCMIntentService(31008): stored iconColor=#92c136 D/PushPlugin_GCMIntentService(31008): stored sound=true D/PushPlugin_GCMIntentService(31008): stored vibrate=true D/PushPlugin_GCMIntentService(31008): using icon from plugin options D/PushPlugin_GCMIntentService(31008): using resources large-icon from gcm D/PushPlugin_GCMIntentService(31008): create actions D/PushPlugin_GCMIntentService(31008): send notification event D/PushPlugin(31008): convert extras to json D/PushPlugin(31008): key = content-available D/PushPlugin(31008): key = payload D/PushPlugin(31008): key = icon D/PushPlugin(31008): key = image D/PushPlugin(31008): key = title D/PushPlugin(31008): key = vibrationPattern D/PushPlugin(31008): key = message D/PushPlugin(31008): key = collapse_key D/PushPlugin(31008): key = foreground V/PushPlugin(31008): extrasToJSON: {"image":"solm","title":"SOLM REQUEST","message":"SOLM required by watch center","additionalData":{"content-available":"1","payload":{"data":{"_id":"5734d773c304e3ea544f0966"},"type":"SOLM_REQUEST","timestamp":1463080819553},"icon":"prescient_smallicon","vibrationPattern":[0,0],"collapse_key":"do_not_collapse","foreground":false}} D/PushPlugin_PushHandlerActivity(31008): not id = 0 V/PushPlugin_PushHandlerActivity(31008): onCreate D/PushPlugin_PushHandlerActivity(31008): callback = null D/PushPlugin_PushHandlerActivity(31008): bringToForeground = true D/PushPlugin(31008): convert extras to json D/PushPlugin(31008): key = content-available D/PushPlugin(31008): key = payload D/PushPlugin(31008): key = callback D/PushPlugin(31008): key = icon D/PushPlugin(31008): key = image D/PushPlugin(31008): key = title D/PushPlugin(31008): key = vibrationPattern D/PushPlugin(31008): key = message D/PushPlugin(31008): key = coldstart D/PushPlugin(31008): key = collapse_key D/PushPlugin(31008): key = foreground V/PushPlugin(31008): extrasToJSON: {"image":"solm","title":"SOLM REQUEST","message":"SOLM required by watch center","additionalData":{"content-available":"1","payload":{"data":{"_id":"5734d773c304e3ea544f0966"},"type":"SOLM_REQUEST","timestamp":1463080819553},"icon":"prescient_smallicon","vibrationPattern":[0,0],"coldstart":false,"collapse_key":"do_not_collapse","foreground":false}} D/PushPlugin_PushHandlerActivity(31008): isPushPluginActive = true D/PushPlugin_PushHandlerActivity(31008): don't want main activity D/PushPlugin_GCMIntentService(31008): onMessage - from: 668220855123 D/PushPlugin_GCMIntentService(31008): normalize extras D/PushPlugin_GCMIntentService(31008): key = content-available D/PushPlugin_GCMIntentService(31008): replace key content-available with content-available D/PushPlugin_GCMIntentService(31008): key = payload D/PushPlugin_GCMIntentService(31008): replace key payload with payload D/PushPlugin_GCMIntentService(31008): key = icon D/PushPlugin_GCMIntentService(31008): replace key icon with icon D/PushPlugin_GCMIntentService(31008): key = image D/PushPlugin_GCMIntentService(31008): replace key image with image D/PushPlugin_GCMIntentService(31008): key = title D/PushPlugin_GCMIntentService(31008): replace key title with title D/PushPlugin_GCMIntentService(31008): key = vibrationPattern D/PushPlugin_GCMIntentService(31008): replace key vibrationPattern with vibrationPattern D/PushPlugin_GCMIntentService(31008): key = message D/PushPlugin_GCMIntentService(31008): replace key message with message D/PushPlugin_GCMIntentService(31008): key = collapse_key D/PushPlugin_GCMIntentService(31008): replace key collapse_key with collapse_key D/PushPlugin_GCMIntentService(31008): background D/PushPlugin_GCMIntentService(31008): message =[SOLM required by watch center] D/PushPlugin_GCMIntentService(31008): title =[SOLM REQUEST] D/PushPlugin_GCMIntentService(31008): contentAvailable =[1] D/PushPlugin_GCMIntentService(31008): create notification E/PushPlugin_GCMIntentService(31008): Number format exception - Error parsing notId: Invalid int: "null" D/PushPlugin_GCMIntentService(31008): stored icon=null D/PushPlugin_GCMIntentService(31008): stored iconColor=#92c136 D/PushPlugin_GCMIntentService(31008): stored sound=true D/PushPlugin_GCMIntentService(31008): stored vibrate=true D/PushPlugin_GCMIntentService(31008): using icon from plugin options D/PushPlugin_GCMIntentService(31008): using resources large-icon from gcm D/PushPlugin_GCMIntentService(31008): create actions D/PushPlugin_GCMIntentService(31008): send notification event D/PushPlugin(31008): convert extras to json D/PushPlugin(31008): key = content-available D/PushPlugin(31008): key = payload D/PushPlugin(31008): key = icon D/PushPlugin(31008): key = image D/PushPlugin(31008): key = title D/PushPlugin(31008): key = vibrationPattern D/PushPlugin(31008): key = message D/PushPlugin(31008): key = collapse_key D/PushPlugin(31008): key = foreground V/PushPlugin(31008): extrasToJSON: {"image":"solm","title":"SOLM REQUEST","message":"SOLM required by watch center","additionalData":{"content-available":"1","payload":{"data":{"_id":"5734d7b8c304e3ea544f0999"},"type":"SOLM_REQUEST","timestamp":1463080888493},"icon":"prescient_smallicon","vibrationPattern":[0,0],"collapse_key":"do_not_collapse","foreground":false}} D/PushPlugin_PushHandlerActivity(31008): not id = 0 V/PushPlugin_PushHandlerActivity(31008): onCreate D/PushPlugin_PushHandlerActivity(31008): callback = null D/PushPlugin_PushHandlerActivity(31008): bringToForeground = true D/PushPlugin(31008): convert extras to json D/PushPlugin(31008): key = content-available D/PushPlugin(31008): key = payload D/PushPlugin(31008): key = callback D/PushPlugin(31008): key = icon D/PushPlugin(31008): key = image D/PushPlugin(31008): key = title D/PushPlugin(31008): key = vibrationPattern D/PushPlugin(31008): key = message D/PushPlugin(31008): key = coldstart D/PushPlugin(31008): key = collapse_key D/PushPlugin(31008): key = foreground V/PushPlugin(31008): extrasToJSON: {"image":"solm","title":"SOLM REQUEST","message":"SOLM required by watch center","additionalData":{"content-available":"1","payload":{"data":{"_id":"5734d7b8c304e3ea544f0999"},"type":"SOLM_REQUEST","timestamp":1463080888493},"icon":"prescient_smallicon","vibrationPattern":[0,0],"coldstart":false,"collapse_key":"do_not_collapse","foreground":false}} D/PushPlugin_PushHandlerActivity(31008): isPushPluginActive = true D/PushPlugin_PushHandlerActivity(31008): don't want main activity

anicarrr commented 8 years ago

@macdonst any idea?

macdonst commented 8 years ago

@anicarrr I can't seem to reproduce this issue. There has to be something different about your setup as tapping on the notification should bring to front.

EvsanDlg commented 8 years ago

Hi! i have the same trouble with app not going to foreground when I tap on notification. Everything else is fine. Android:launchMode set to "singleTop". Use correct payload (all is in data field), no content-available option. Has Android 5.1 (Samsung i9100G with CM12.1), Cordova cli 5.4.1 and phonegap plugin push v1.6.3. If it matters, I develop my app with Intel XDK build3240 and use theese plugins in my crosswalk project (besides push plugin): cordova-plugin-statusbar v2.1.0,cordova-plugin-device v1.1.1, cordova-plugin-splashscreen v3.2.0

macdonst commented 8 years ago

@EvsanDlg I can't reproduce. I know that folks have had issues with a certain build of Cyanogen.

anicarrr commented 8 years ago

Hi @macdonst. I've figured out something. I will show you my server side implentation: I have two diffferent behaviours depending on how I implement the GCM services.

When the app is in background:

In the following code when I touch the notification the app goes from background to foreground BUT the content available doesn't work and the image icon become material design (circle image) and no small icon appears.

var GCM = require('./gcm-ccs');
var config = require('../../config/environment').androidPush;
var PushMessage = require('../../api/push-message/push-message.model');

exports.notify = function(token, message, userPreferences) {
    var gcm = GCM(config.appId, config.appKey);

    gcm.on('error', function(err) {
        console.log('GCM-ERROR: ', err);
    });

    gcm.on('receipt', function(messageId, from, category, data) {
        console.log('received receipt', arguments);
        PushMessage.updateMessageStatus({messageId: arguments['3'].original_message_id, status:'received'});
    });

    var notification = {};
    if(message.description || message.title){
        notification = { body: message.description ? message.description : '', title: message.title ? message.title : 'App title' };
    }

    var data =  { payload: { type: message.type, data: message.data, timestamp: message.timestampVersion }, "content-available": "1"};

    if(message.image){
        data.image = message.image;
    }else{
        data.image = "notification";
    }

    if(!userPreferences.alertType.vibration){
        data.vibrationPattern = [0, 0];
    }

    if(!userPreferences.alertType.sound){
        data.soundname = "nosound";
    }

    gcm.send(token, message._id, data, { delivery_receipt_requested: true, notification: notification }, function(err, messageId, to) {
        if (!err) {
            console.log('sent message to', to, 'with message_id =', messageId);
            PushMessage.updateMessageStatus({messageId: messageId, status:'sent'});
        } else {
            console.log('failed to send message', err);
        }
    });

}

In the following code the icon image appear as it is (and square image) and the small icons too. The content available works BUT when I tap the notification the app doesn't goes from background to foreground and the notification logic (onNotification event at client side) triggers twice (when I receive the notification and when I tapped it).

var GCM = require('./gcm-ccs');
var config = require('../../config/environment').androidPush;
var PushMessage = require('../../api/push-message/push-message.model');

exports.notify = function(token, message, userPreferences) {
    var gcm = GCM(config.appId, config.appKey);

    gcm.on('error', function(err) {
        console.log('GCM-ERROR: ', err);
    });

    gcm.on('receipt', function(messageId, from, category, data) {
        console.log('received receipt', arguments);
        PushMessage.updateMessageStatus({messageId: arguments['3'].original_message_id, status:'received'});
    });

    var notification = {};
    if(message.description || message.title){
        notification = { message: message.description ? message.description : '', title: message.title ? message.title : 'App title' };
    }

    var data =  { 
        payload: { 
            type: message.type, 
            data: message.data, 
            timestamp: message.timestampVersion 
        }, 
        "content-available": "1",
        message: notification.message;
        title: notification.title;
    };

    if(message.image){
        data.image = message.image;
    }else{
        data.image = "notification";
    }

    if(!userPreferences.alertType.vibration){
        data.vibrationPattern = [0, 0];
    }

    if(!userPreferences.alertType.sound){
        data.soundname = "nosound";
    }

    gcm.send(token, message._id, data, { delivery_receipt_requested: true }, function(err, messageId, to) {
        if (!err) {
            console.log('sent message to', to, 'with message_id =', messageId);
            PushMessage.updateMessageStatus({messageId: messageId, status:'sent'});
        } else {
            console.log('failed to send message', err);
        }
    });

}

The differences here is that I deleted the notification object from the fourth argument at "gcm.send" function and I declared a title and message properties in the "data" object.

Any Ideas?

EvsanDlg commented 8 years ago

@macdonst , need to mention, that I described the situation while trying to debug my app with Intel XDK. But after I built it and installed independently, the problem vanished. Everything is good. Sorry for disturbing, you do a great job!

macdonst commented 8 years ago

@EvsanDlg np, Tony from the IntelXDK team recently came in and did a great write up of using this plugin on their platform. I added it to the docs under IntelXDK Support.

macdonst commented 8 years ago

@anicarrr yeah, delete the notification part of your push message. That will cause the OS to take over the handling. As for the double notification event that makes sense as you'll get it once because content-available is set and a second time due to the tap. Although that tap should bring the app to the foreground.

Where can I get the node module gcm-ccs? What version of Android is your Moto X running?

anicarrr commented 8 years ago

@macdonst Here is the module: https://www.npmjs.com/package/node-gcm-ccs I'm running android 5.1

anicarrr commented 8 years ago

@macdonst Hi, just to tell you that for some reason I had the lanchmode set as "singleInstance". I've changed to "singleTop" and now it works! Pretty wierd because when you told me to check that out I had it set as "SingleTop". Anyway, it is working now.

In the other hand, it is pretty annoying that the logic execute twice, one for income notification and the other for tapping it. Could you do something about it? Like adding a new property in "additionaData" object that says that the notification was triggered by tapping! I have many problems because of that. I have to handle it myself.

macdonst commented 8 years ago

@anicarrr nope, you gotta handle that yourself. What I suggest to people is to put their own unique identifier in the push payload and you can read it in additionalData. Alternatively, just make it a silent push, i.e. omit tite/message, so the UI is never shown.

lock[bot] commented 6 years ago

This thread has been automatically locked.