我正在使用PushPlugin用于cordova,而在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'param值(确实可以播放声音) -
{
"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)”功能块。
最后,我想要的是非常简单 - 在应用未运行或应用处于后台时,在推送通知上播放自定义声音文件。 < / p>
提前致谢。
答案 0 :(得分:4)
我观察了Cordova PushPlugin添加到android平台项目的java代码,查看文件&#34; GCMIntentService&#34;。在&#39; onMessage&#39;功能,我看到了这个 -
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端代码的行。
您可能已经注意到,问题是只有在应用isInForeground()
。
起初,我认为我应该在else
块内,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);
}
}
它有效,但它不是一个完美的解决方案,我宁愿在后台处理通知或在我的webview中销毁INSIDE THE JAVASCRIPT代码,而不是Java。希望此功能将添加到PushPlugin。
也许至少他们可以为&#34; soundname&#34;添加一个参数。在他们打电话给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'
...
}
答案 1 :(得分: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目录中。
答案 2 :(得分: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);
}
}
答案 3 :(得分:0)
在gcm配置中发送消息(服务器端),您需要添加配置: &#39;数据&#39; =&GT; &#39;声音&#39; =&GT; &#34;默认&#34;,