应用程序处于前台时,Cordova PushPlugin无法正常工作

时间:2015-07-24 20:47:04

标签: android cordova push-notification phonegap-pushplugin

我正在使用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%确定这是放置它的地方。

之前有人遇到过这个问题吗?知道我在这里可能缺少什么吗?

谢谢,

卢西亚诺

2 个答案:

答案 0 :(得分:1)

  

但是当它处于前台时,在收到通知时没有任何反应

这是PushPlugin的默认行为。只有当应用程序在后台或关闭时,它才会创建通知和相关声音。

  

希望让它播放声音,甚至显示弹出提示,让用户知道他有新的通知。

最简单的解决方案是使用Fiddle。你所要做的就是:

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

请注意,此蜂鸣声将是用户在其设备上配置的信息音。

  

不确定应该放置beep.wav文件的位置,我将其放入www文件夹

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

另请参阅:Cordova Plugin Dialogs

答案 1 :(得分:1)

对于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": {}
                    });