【问题标题】:Cordova PushPlugin not working when app is in foreground当应用程序处于前台时,Cordova PushPlugin 不起作用
【发布时间】:2015-07-24 20:47:03
【问题描述】:

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

当应用程序处于后台时,一切正常,并在我的 droid 通知栏中收到通知,但是当它在前台时,收到通知时没有任何反应。想让它播放声音,甚至显示弹出警报,让用户知道他有新通知。从插件的 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% 确定该放在哪里。

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

谢谢,

卢西亚诺

【问题讨论】:

    标签: android cordova push-notification phonegap-pushplugin


    【解决方案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": {}
                        });
    

    【讨论】:

      【解决方案2】:

      但是当它在前台时,收到通知时什么也没有发生

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


      想让它播放声音,甚至显示弹出警报,让用户知道他有新通知。

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

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

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


      不确定 beep.wav 文件应该放在哪里,我把它放在 www 文件夹中

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

      另见:Android won't play push sound while app not running

      【讨论】:

      • @AngelGris,我也遇到了同样的问题,你能帮我检测一下应用程序是在前台还是前台运行吗?
      • @AngelGris,我已经添加了答案,这对我有用..希望对你也有帮助:)
      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 2019-01-11
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2017-08-11
      相关资源
      最近更新 更多