科尔多瓦PushPlugin:虽然应用程序没有运行Android不会起到推的声音 [英] Cordova PushPlugin: Android won't play push sound while app not running

查看:113
本文介绍了科尔多瓦PushPlugin:虽然应用程序没有运行Android不会起到推的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 PushPlugin 的科尔多瓦,并在Android中,我不能让推送通知播放声音,而应用程序没有运行,或者在后台(在状态栏上的旗帜,是psented OK $ P $)。 下面是被调用在Android推送通知功能 -

 函数onNotification(五){
    。
    。
    。
        案消息:
        {
            VAR玛雅=新媒体(/ android_asset / WWW / RES /生/ tritone.mp3);
            myMedia.play({numberOfLoops:2})
 

播放声音细而应用程序在前台运行。

这是函数onNotification(E)的E参数值收到推后,而我在前台(这确实起到了声精) -

  {
   消息:正文......
   有效载荷:{
      消息:正文......
      soundname:/ android_asset / WWW / RES /生/ tritone.mp3
      头衔:sometitle
   },
   collapse_key:do_not_collapse,
   从:969601086761,
   soundname:/ android_asset / WWW / RES /生/ tritone.mp3
   前景:真实,
   事件:消息
}
 

我有一种感觉,功能onNotification(五)块不叫所有,而应用程序没有运行,或者在后台。

最后,我要的是很简单 - 打推送通知的自定义声音文件而应用程序没有运行,或应用程序是在后台。

在此先感谢。

解决方案

我观察了Java code加入到了Android平台项目由科尔多瓦PushPlugin,查看文件GCMIntentService

。在的onMessage功能,我看到了这一点 -

 如果(PushPlugin.isInForeground()){
    extras.putBoolean(前台,真正的);
    PushPlugin.sendExtras(临时演员);
}
其他 {
    extras.putBoolean(前台,假);

    //发送通知,如果有消息
    如果(extras.getString(消息)=&零安培;!&安培;!extras.getString(消息)的长度()= 0){
        createNotification(背景下,临时演员);
    }
}
 

然后,它变得清晰,看行 PushPlugin.sendExtras(演员)?这是触发的JavaScript web视图一侧的code线。 正如你可能已经注意到了,问题是,这条线只调用如果应用程序 isInForeground()

起初,我想我也应该加入这一行里的其他块,右线下 extras.putBoolean(前台, FALSE),但只能在一个情况下的应用程序实际上是在后台帮助,这将不利于如果应用程序没有运行在所有。

以上的原因,我会简单地播放音频文件中的Java code,这样的(测试工作) -

 如果(PushPlugin.isInForeground()){
    extras.putBoolean(前台,真正的);
    PushPlugin.sendExtras(临时演员);
}
其他 {
    extras.putBoolean(前台,假);

    的MediaPlayer媒体播放器= MediaPlayer.create(背景下,R.raw.mytone);
    mediaPlayer.start();

    //发送通知,如果有消息
    如果(extras.getString(消息)=&零安培;!&安培;!extras.getString(消息)的长度()= 0){
        createNotification(背景下,临时演员);
    }
}
 

它的工作原理,但它不是一个完美的解决方案,我将有preferred办通知的处理,而在背景或破坏内部的JAVASCRIPT code在我的web视图,而不是在Java中。希望这功能将被添加到PushPlugin

也许他们至少可以为soundname在他们的呼叫发挥到 createNotification(上下文的背景下,捆绑演员)添加参数,这也将是一个不错的足够的解决方案,使用这个插件我。

澄清:

我用下面的code播放通知声音文件的名称:

 字符串soundName = extras.getString(soundname);
AssetFileDescriptor AFD = getAssets()openFd(soundName)。
的MediaPlayer播放器=新的MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
播放器prepare()。
player.start();
 

在服务器端,我会通过一个JSON看起来像这样送为Android(IOS有不同的键)时:

  {
  ...
  soundname:WWW / RES /生/ yoursoundfile.mp3
  ...
}
 

I'm using the PushPlugin for cordova, and in android, I can't make the push notification play a sound while the app is not running or in background (the banner in status bar is presented ok). Here's the function that gets called on android push notifications-

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

the sound plays fine while app is running in foreground.

This is the 'e' param value of the function "onNotification(e)" after a push is received while I'm in foreground (which does play the sound fine)-

{  
   "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"
}

I have a feeling that the "function onNotification(e)" block is not called at all while the app is not running or in background.

In the end, what I want is very simple- to play a custom sound file on push notification while app is not running, or app is in background.

Thanks in advance.

解决方案

I observed the java code added to the android platform project by the Cordova PushPlugin, looking at the file "GCMIntentService". in the 'onMessage' function, I saw this-

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);
    }
}

Then it became clear, see the line PushPlugin.sendExtras(extras)? this is the line that triggers the code on the javascript webview side. As you might have noticed, the problem is that this line is only called if the app isInForeground().

At first, I thought I should add this line also inside the else block, right below the line extras.putBoolean("foreground", false), but that would only help in a situation where the app is actually in background, it won't help if the app is not running at all.

for the reason above, I will simply play the audio file in Java code, like this (tested and working)-

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);
    }
}

It works, but it's not a perfect solution, I would have preferred to do the handling of notifications while in background or destroyed INSIDE THE JAVASCRIPT code in my webview, and not in Java. hopefully this functionality will be added to the PushPlugin.

Maybe at least they can add a parameter for "soundname" to be played in their call to createNotification(Context context, Bundle extras), which would also be a good enough solution for me using this plugin.

Clarification:

I used the following code to play the notification sound file name:

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(); 

On the server side, I would pass a JSON looking like this when sending for Android (iOS has different keys):

{
  ...
  soundname: 'www/res/raw/yoursoundfile.mp3'
  ...
}

这篇关于科尔多瓦PushPlugin:虽然应用程序没有运行Android不会起到推的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆