How can we get GCM push notifications in a phonegap android application even when the app goes idle or is in background.
"katzer/cordova-plugin-background-mode" doesn't seems to work...
"I'am getting the push notifications successfully when the app runs foreground"
cordova version:4.3.0 android 4.4 phonegap 4.2.0
I'll copy my notification functions below... on deviceready
function onDeviceReady() {
try
{
pushNotification = window.plugins.pushNotification;
jQuery("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
if (device.platform == 'android' || device.platform == 'Android' ||
device.platform == 'amazon-fireos' ) {
pushNotification.register(successHandler, errorHandler, {"senderID":"my-project-id","ecb":"onNotification"});
} else {
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"}); // required!
}
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
alert(txt);
}
}
and
function onNotification(e) {
switch( e.event )
{
case 'registered':
if ( e.regid!='' )
{
android_reg_id = e.regid;
jQuery.ajax({
type:"POST",
url:SITEURL+"index.php?r=Manageuser/App_Reg_Android",
data:{regid: android_reg_id,employeeno:employeeno}
}).done(function(msg) {
});
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground)
{
//jQuery("#app-status-ul").html('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
// on Android soundname is outside the payload.
// On Amazon FireOS all custom attributes are contained within payload
var soundfile = e.soundname || e.payload.sound;
// if the notification contains a soundname, play it.
// playing a sound also requires the org.apache.cordova.media plugin
var my_media = new Media("/www/"+ soundfile);
my_media.play();
}
else
{
if (e.coldstart)
$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
else
$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
//location.href = e.payload.redid;
// otherwise we were launched because the user touched a notification in the notification tray.
}
jQuery("#app-status-ul").html('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
//window.localStorage.setItem("push_que", e.payload.redid);
//location.href = e.payload.redid;
break;
case 'error':
jQuery("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
break;
default:
jQuery("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}
Plugin that is used is com.phonegap.plugins.PushPlugin 2.4.0 "PushPlugin"