0

I am doing pushnotification for both Android/IOS.

I have used a cordova push-plugin https://github.com/phonegap-build/PushPlugin, it seems to work great.

Info : I'm on a AngularJS project.

In my NotificationHelper factory i have this init method:

helper.init = function() {

    // Instanciate push plugin notification
    var pushNotification = window.plugins.pushNotification;

    var errorHandler = function(error) {
        logger.debug('errorHandler = ' + error);
    };

    if ($rootScope.isAndroid()) {
        var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID;
        pushNotification.register(function(result) {
            logger.debug('successHandler = ' + result);
        }, errorHandler, {
            'senderID' : senderId,
            'ecb' : 'onNotificationGCM'
        });
    }
};

I also defined those methods on mains.js :

var onNotificationGCM = function(event) {
   // call back to web service in Angular.
   var elem = angular.element(document.querySelector('[ng-app]'));
   var injector = elem.injector();
   var service = injector.get('NotificationHelper');
   service.onNotificationGCM(event);
};

It's a trick to call angularJS factory from main javascript.

'onNotificationGCM' call the 'NotificationHelper.onNotificationGCM' method :

helper.onNotificationGCM = function(e) {
    switch (e.event) {
    case 'message':
        // Notification happened while app was in the foreground
        if (e.foreground) {
            logger.debug('[notification] [message] Foreground : ' + JSON.stringify(e));
        } else { // Notification touched in the notification tray
            logger.debug('[notification] [message] Background : ' + JSON.stringify(e));
            if (e.coldstart) {
                // App was not running and user clicked on notification
            } else {
                // App was running and user clicked on notification
            }
        }
        decriptPayloadNotification(e.payload);
        break;
    case 'registered':
        logger.debug('[notification] [registered] : ' + JSON.stringify(e));
        if (e.regid.length > 0) {
            registerUser(e.regid, 'gcm');
        }
        break;

    case 'error':
        logger.debug('[notification] [error] : ' + JSON.stringify(e));
        break;

    default:
        logger.debug('[notification] [default] : Unknown, an event was received and we do not know what it is : ' + JSON.stringify(e));
        break;
    }
};

During the first use, everything work good :

  • 'Registered' event is received
  • Notifications are received
  • When i'm on foreground I receive 'message' event :

NotificationHelper: [notification] [message] Foreground : {"event":"message","from":"847593779913","message":"Agenêts 23/03 10h\r\n","collapse_key":"do_not_collapse","foreground":true,"payload":{"lt":"school","lv":"E1154","notId":"35429","title":"Agenêts le 23/03/2015","message":"Agenêts 23/03 10h\r\n"}}

  • When i'm on background, if i receive notif and touche it on notification tray, I receive 'message' event :

NotificationHelper: [notification] [message] Background : {"event":"message","from":"847593779913","message":"la piscine sera fermée 1 journée pour raison technique","coldstart":false,"collapse_key":"do_not_collapse","foreground":false,"payload":{"lt":"swimming pool","lv":"E114","notId":"29869","title":"23/04/2015 fermeture de la piscine","message":"la piscine sera fermée 1 journée pour raison technique"}}

BUT, if i kill my app, everything stop to work.

If i restart app, i will not receive anymore 'message'event and 'onNotificationGCM' will not be called.

I founded some articles speaking about this problem, but without success :

Stackoverflow : Phonegap PushNotification to open a specific app page

Does anyone has an idea about this problem ?

Community
  • 1
  • 1
Geoffrey Lalloué
  • 1,456
  • 1
  • 20
  • 43

2 Answers2

0

If you are using node-gcm as push notification server side I hope this might help for your question: things to check:

  • use adb logcat from console and check your device logs to see if your device receives any notification (you should see some logs from gcmservices if you receive). I believe you should see some logs because otherwise you probably wouldn't get any notification when the app is on foreground either.
  • check if you add "title" and "message" keys on your message data (this draw me crazy before)
  • check if delayWhileIdle parameter is false

sample message variable and method i am using is below:

var pushAndroidSender = new gcm.Sender('Your GoogleApi Server Key Here');
var message = new gcm.Message({
    collapseKey: 'demo',
    delayWhileIdle: false,
    timeToLive: 3,
    data: {
        "message":"What's up honey?",
        "title":"Come on"
    }
});
pushAndroidSender.send(message, registrationIds, function(err, result) {
    if (err) {
        console.error("this is an error: " + err);
    } else {
        console.log("this is a successful result:" + result);
    }
});
  • I guess only `message` is required. `title` is optional – Ajoy May 02 '15 at 04:58
  • I did what you said. 1) In adb logcat I can see some logs from gcmservices. 2) I have "title" and "message" on my message data. 3) And I set "delayWhileIdle" to false. But I am having the same problem :( – Geoffrey Lalloué May 22 '15 at 14:53
0

I was asking myself why it worked correctly the first time and not the following times...and i had this idea :

  • maybe callback function is subscribed to plugin first time but not the next times.

And this is exactly the source of my problem. I call "pushNotification.register" only one time to init plugin and get ids... But the next time I launch app, i don't instanciate again the plugin, so pushplugin don't know which method to call in callback request.

So answer is : Call "pushNotification.register" in each application launch !

In my code, i have to call method below on each "deviceready" event :

helper.init = function() {

// Instanciate push plugin notification
var pushNotification = window.plugins.pushNotification;

var errorHandler = function(error) {
    logger.debug('errorHandler = ' + error);
};

if ($rootScope.isAndroid()) {
    var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID;
    pushNotification.register(function(result) {
        logger.debug('successHandler = ' + result);
    }, errorHandler, {
        'senderID' : senderId,
        'ecb' : 'onNotificationGCM'
    });
}
};
Geoffrey Lalloué
  • 1,456
  • 1
  • 20
  • 43