我已经成功实现了 PushPlugin 并且它在所有状态下触发推送通知,除非应用程序在前台。这是我正在使用的代码:
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;
}
}
我环顾四周,没有发现任何与此问题相关的内容,修复似乎应该很简单,但暗指我,因为我缺乏使用此插件的经验。有没有人遇到这个问题,可以告诉我问题出在哪里,在此先感谢。
编辑:我一直在通过usb调试调试应用程序,我发现应用程序正在接收警报但它没有触发任何东西,我已经向“if(e.foreground){alert(”你有收到新通知:“+ e.payload.message);” 但它仍然没有触发,问题显然在于 if 语句,但我无法弄清楚。
编辑 2:我已更新问题以回应用户 Hana Le 的评论,我收到警告错误:“processMessage failed: Error: ReferenceError: onNotification is not defined”
这需要在哪里定义才能使其工作,为什么应用程序在后台运行和根本不运行时会接收并触发通知?