2

我正在为cordova使用PushPlugin,在android中,当应用程序未运行或在后台(状态栏中的横幅显示正常)时,我无法让推送通知播放声音。这是在 android 推送通知上调用的函数-

function onNotification(e) {
    .
    .
    .
        case 'message':
        {    
            var myMedia = new Media("/android_asset/www/res/raw/tritone.mp3");
            myMedia.play({ numberOfLoops: 2 })

应用程序在前台运行时声音播放正常。

这是在我处于前台时收到推送后函数“onNotification(e)”的“e”参数值(它确实可以很好地播放声音)-

{  
   "message":"body text...",
   "payload":{  
      "message":"body text...",
      "soundname":"/android_asset/www/res/raw/tritone.mp3",
      "title":"sometitle"
   },
   "collapse_key":"do_not_collapse",
   "from":"969601086761",
   "soundname":"/android_asset/www/res/raw/tritone.mp3",
   "foreground":true,
   "event":"message"
}

我有一种感觉,当应用程序未运行或在后台运行时,根本不会调用“函数 onNotification(e)”块。

最后,我想要的非常简单 - 在应用程序未运行或应用程序处于后台时在推送通知上播放自定义声音文件。

提前致谢。

4

4 回答 4

4

我观察了 Cordova PushPlugin 添加到 android 平台项目的 java 代码,查看文件“GCMIntentService”。在“onMessage”函数中,我看到了这个——

if (PushPlugin.isInForeground()) {
    extras.putBoolean("foreground", true);
    PushPlugin.sendExtras(extras);
}
else {
    extras.putBoolean("foreground", false);

    // Send a notification if there is a message
    if (extras.getString("message") != null && extras.getString("message").length() != 0) {
        createNotification(context, extras);
    }
}

然后就很清楚了,看到这条线了PushPlugin.sendExtras(extras)吗?这是在 javascript webview 端触发代码的行。正如您可能已经注意到的那样,问题在于只有在 app isInForeground().

起初,我想我应该在else块内添加这条线,就在 line 的正下方extras.putBoolean("foreground", false),但这只会在应用程序实际上处于后台的情况下有所帮助,如果应用程序根本没有运行,它就无济于事。

出于上述原因,我将简单地用 Java 代码播放音频文件,就像这样(经过测试和工作)-

if (PushPlugin.isInForeground()) {
    extras.putBoolean("foreground", true);
    PushPlugin.sendExtras(extras);
}
else {
    extras.putBoolean("foreground", false);

    MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.mytone);
    mediaPlayer.start();

    // Send a notification if there is a message
    if (extras.getString("message") != null && extras.getString("message").length() != 0) {
        createNotification(context, extras);
    }
}

它可以工作,但它不是一个完美的解决方案,我宁愿在后台处理通知或在我的 web 视图中的 JAVASCRIPT 代码中销毁通知,而不是在 Java 中。希望这个功能将被添加到 PushPlugin 中。

也许至少他们可以为“soundname”添加一个参数,以便在他们的调用中播放createNotification(Context context, Bundle extras),这对于我使用这个插件来说也是一个足够好的解决方案。

澄清:

我使用以下代码播放通知声音文件名:

String soundName = extras.getString("soundname");               
AssetFileDescriptor afd = getAssets().openFd(soundName);
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start(); 

在服务器端,我会在发送 Android 时传递一个如下所示的 JSON(iOS 有不同的键):

{
  ...
  soundname: 'www/res/raw/yoursoundfile.mp3'
  ...
}
于 2014-07-22T09:35:06.277 回答
1

官方存储库中有一个 Pull Request 从未被合并,但允许您指定要从有效负载播放的 mp3 文件它工作得很好。唯一的缺点是必须把mp3文件放到android/res/raw目录下(不能放到www目录下)

看看这里 https://github.com/phonegap-build/PushPlugin/pull/301

您所要做的就是发送一个有效负载 data.payload = {sound: 'myAlert', message:'blabla'}(注意删除有效负载中的 .mp3 扩展名)并将 myAlert.mp3 放在您的 cordova 项目的 platform/android/res/raw 目录中。

于 2014-10-20T13:47:57.310 回答
0

只需编辑文件“GCMIntentService”。在“onMessage”功能中。在 'else' 中调用 PushPlugin.sendExtras(extras) 方法

工作代码

if (extras != null)
        {
            // if we are in the foreground, just surface the payload, else post it to the statusbar
            if (PushPlugin.isInForeground()) {
                extras.putBoolean("foreground", true);
                PushPlugin.sendExtras(extras);
            }
            else {
                extras.putBoolean("foreground", false);
                PushPlugin.sendExtras(extras);

                // Send a notification if there is a message
                if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                    createNotification(context, extras);
                }
            }
于 2015-05-13T06:18:43.577 回答
0

在gcm配置发送消息(服务器端)需要添加配置:'data' => 'sound' => "default",

于 2016-02-17T13:47:55.970 回答