I'm using the Cordova Push Notifications Plugin 1.3.4 with my Cordova/Phonegap App. Unfortunately, when receiving a push notification, the ecb callback in my JavaScript is never fired and I can't handle the push notification (not even when the app is running in foreground).
I'm using the example code from the demo:
pushNotification.register(tokenHandler, errorHandler, {"badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN"});
The registration is successful, but the following callback is never triggered:
function onNotificationAPN (event) {
if (event.alert)
{
navigator.notification.alert(event.alert);
}
}
The problem is the way you defined your callback function, causing the Push Plugin's evaluation of your callback (i.e., via [webView stringByEvaluatingJavaScriptFromString
) to fail, since it will not be aware of it.
If you define your callback function as a global object instead, the plugin will trigger your callback correctly every time a new notification arrives:
onNotificationAPN = function(event) {
if (event.alert)
{
navigator.notification.alert(event.alert);
};
};
For Android, you'd define your onNotificationGCM
callback the same way.
Mobiletainment's answer fixed it for me! I've been searching all afternoon for the solution to this, albeit for the GCM callback, but the functionality is the same.
I'd upvote Mobiletainment, but I'm new here and it won't let me, but thanks!
For anyone that may be searching for a GCM solution (which there are many in the Googleverse without many good answers) here was my code that finally worked:
// handle GCM notifications for Android
onNotificationGCM = function(e){
alert('onGCM');
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
console.log("Regid " + e.regid);
//Do registration work here...
}
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;
}
}
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