当应用程序处于前台时,Cordova PushPlugin 不工作

Cordova PushPlugin not working when app is in foreground

我正在使用 Cordova 3.5.0-0.2.6 创建一个 Android 应用程序并使用 PushPlugin 2.2.1 推送通知。

一切正常,当应用程序在后台时,我的机器人通知栏中会收到通知,但当它在前台时,收到通知时什么也没有发生。想让它播放声音,甚至显示弹出警报,让用户知道他有新通知。从插件的 github 页面 (https://github.com/phonegap-build/PushPlugin) 复制了示例代码,但它不起作用。对应的部分代码是这样的:

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)
        {
            // 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("/android_asset/www/"+ soundfile);
            my_media.play();
        }
        else
        {   // otherwise we were launched because the user touched a notification in the notification tray.
            window.location = 'iframe.html?url=' + e.payload.url;
            if (e.coldstart) {
                //$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
            } else{
                //$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
            }
        }

不确定 beep.wav 文件应该放在哪里,我把它放在了 www 文件夹中,但我 100% 确定那是放它的地方。

以前有人遇到过这个问题吗?知道我在这里可能遗漏了什么吗?

谢谢,

卢西亚诺

but when it's in foreground nothing happens when it get the notification

这是 PushPlugin 的默认行为。它仅在应用程序处于后台或关闭时创建通知和相关声音。


Wanted to make it play a sound or even show a popup alert to let the user know he has a new notification.

最简单的解决方案是使用 Cordova Plugin Dialogs。您所要做的就是:

switch( e.event )
{
    case 'message':
        if (e.foreground) {
            navigator.notification.beep(1);
        }
.
.
}

请注意,此蜂鸣声将是用户在 his/her 设备上配置的消息音。


Not sure where the beep.wav file should be placed, I placed it into the www folder

也不确定,但我认为它也是 www 目录。

另请参阅:Android won't play push sound while app not running

对于 android 请添加 属性 "forceShow": "true" 它应该可以工作..

示例代码如下:

var push = PushNotification.init({
                        "android": {
                            "senderID": "xxxxxxxxx",
                            "forceShow": "true",
                            "icon": "icon",
                            "sound": true,
                            "vibration": true,
                            "badge": true
                        },
                        "browser": {
                        },
                        "ios": {
                            "senderID": "xxxxxxxxx",
                            "icon": "icon",
                            "gcmSandbox": true,
                            "sound": true,
                            "vibration": true,
                            "badge": true
                        },
                        "windows": {}
                    });