I'm implementing Push Notification in a Phonegap Android app. I'm following the tutorial here. In the tutorial, the onDeviceready function looks like this:
onDeviceReady: function() {
app.receivedEvent('deviceready');
var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"836557454855","ecb":"app.onNotificationGCM"});
},
This means that every time the app starts up, it is registered with Google for Push Notification. I presume this just needs to be done once. So in mine, I have:
onDeviceReady: function() {
var device_id = window.localStorage.getItem('mountmercy_device_id');
//if device_id is in local storage, then it means registration
// with google has already taken place. If not, then register
if(typeof(device_id)==='undefined' || device_id===null){
var pushNotification = window.plugins.pushNotification;
if (window.device.platform == 'android' || window.device.platform == 'Android') {
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});
}
else{
//so its apple
pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
}
}
Then in onNotificationGCM, I set local storage so the device is not registered again:
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
/*
save reg id to server and store response in local storage
...
...
...
*/
window.localStorage.setItem('mountmercy_device_id', data.id);
window.localStorage.setItem('mountmercy_api_key', data.get('api_key'));
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;
case 'error':
//alert('GCM error = '+e.msg);
break;
default:
// alert('An unknown GCM event has occurred');
break;
}
},
The problem occurs when the phone receives a new push notification. In the original project from the tutorial, when the user clicks on the notification message, the app opens and the user sees the alert: "message = blachblah msgcnt = blahblah". This is because the code in the "message" case in onNotificationGCM() is executed.
In my app, the app opens but the code in "message" case is not executed. This is because, in onDeviceReady(), I only register the device with Google once. If I remove the condition:
if(typeof(device_id)==='undefined' || device_id===null){
and register the deice every time, the "message" case is executed. But this seems wrong to have to register the device every time in onDeviceReady(). Is there another solution?
I see in the documentation for Google Cloud Messaging it says
Google may periodically refresh the registration ID
In that case, I register with Google every time the app is opened, and update the returned registration ID in case it has changed. This also solves the problem of handling a push message when it's clicked on in notification bar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With