I have successfully implemented PushPlugin and it is triggering push notifications in all states except when the app is in the foreground. Here is the code I am using:
function onNotification(e)
{
$("#app-status-ul").append('<li>EVENT -> RECEIVED: ' + e.event + '</li>');
switch(e.event)
{
case 'registered':
if (e.regid.length > 0) {
$("#app-status-ul").append('<li>REGISTERED -> REGID: ' + e.regid + '</li>');
console.log("RegID = " + e.regid);
}
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){
$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
//if the notification contains a soundname, play it.
//navigator.notification.alert('--INLINE NOTIFICATION--');
alert("You have a new alert!: " + e.payload.message);
var my_media = new Media (../homebound.wav);
my_media.play();
} else {
//otherwise we were launched because the user touched the notification in the notification tray.
if (e.coldstart)
{
navigator.notification.alert('--COLDSTART NOTIFICATION--');
}
else
{
navigator.notification.alert('--BACKGROUND NOTIFICATION--')
}
}
$("#app-status-ul").append('<li>MESSAGE -> MSG ' + e.payload.message + '</li>');
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>')
$status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</li>');
break;
case 'error':
$("#app-status-ul").append('<li>ERROR -> MSG: ' + e.msg + '</li>');
break;
default:
$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}
I have looked around and not found anything for this issue, the fix seems like it should be simple but is alluding me because I lack experience with this plugin. Has anyone had this issue and can let me know where the issue lies, thanks in advance.
EDIT: I have been debugging the app through usb debug, and I have found that the app is receiving the alert but it is not triggering anything, I have added an alert to the "if(e.foreground){alert("You have received a new notification: " + e.payload.message);" but it still isn't triggering, the issue is obviously lying around the if statement, but I cannot figure it out.
EDIT 2: I have updated to the question to respond to the comment made by user Hana Le, I am getting a warning error: "processMessage failed: Error: ReferenceError: onNotification is not defined"
Where would this need to be defined in order to make it work and why does the app receive and trigger a notification when running in background and when not running at all?