Cordova PushPlugin:Android赢得';应用程序未运行时,无法播放推送声音
我使用的是cordova,在android中,我无法在应用程序未运行或处于后台时播放推送通知(状态栏中的横幅显示为ok)。 以下是在android推送通知中调用的函数-Cordova PushPlugin:Android赢得';应用程序未运行时,无法播放推送声音,android,cordova,push-notification,google-cloud-messaging,phonegap-pushplugin,Android,Cordova,Push Notification,Google Cloud Messaging,Phonegap Pushplugin,我使用的是cordova,在android中,我无法在应用程序未运行或处于后台时播放推送通知(状态栏中的横幅显示为ok)。 以下是在android推送通知中调用的函数- function onNotification(e) { . . . case 'message': { var myMedia = new Media("/android_asset/www/res/raw/tritone.mp3");
function onNotification(e) {
.
.
.
case 'message':
{
var myMedia = new Media("/android_asset/www/res/raw/tritone.mp3");
myMedia.play({ numberOfLoops: 2 })
应用程序在前台运行时,声音播放良好
这是函数“onNotification(e)”的“e”参数值,当我在前台时收到一个推送后(它确实播放声音很好)-
我有一种感觉,当应用程序未运行或处于后台时,根本不会调用“函数onNotification(e)”块
最后,我想要的非常简单——在应用程序未运行或应用程序处于后台时,在推送通知中播放自定义声音文件。
提前感谢。我观察了Cordova PushPlugin添加到android平台项目中的java代码,查看了文件“gcminentService”。在“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端代码的行。
正如您可能已经注意到的,问题在于,只有当应用程序ISINFORGROUND()
时,才会调用此行
起初,我认为我应该在else
块中添加这一行,就在extras.putBoolean(“前台”,false)
行的正下方,但这只会在应用程序实际位于后台的情况下有所帮助,如果应用程序根本没有运行,则不会有任何帮助
出于上述原因,我将简单地用Java代码播放音频文件,如下所示(经过测试并正常工作)-
它可以工作,但不是一个完美的解决方案,我更愿意在后台处理通知,或者在webview的JAVASCRIPT代码中销毁通知,而不是在Java中。希望此功能将添加到PushPlugin中
也许至少他们可以为“soundname”添加一个参数,以便在调用createNotification(上下文上下文,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有不同的键):
官方存储库中有一个从未合并过的拉取请求,但允许您指定要从有效负载播放的mp3文件 很好用。唯一的缺点是您必须将mp3文件放在android/res/raw目录中(您不能将其放在www目录中) 看看这里 你所要做的就是发送一个有效载荷
data.payload={sound:'myAlert',message:'blablabla'}
(注意删除有效负载中的.mp3扩展名),并将myAlert.mp3放在cordova项目的platform/android/res/raw目录中。只需编辑文件“gcminentservice”。在“onMessage”函数中。在'else'中调用方法PushPlugin.sendextas(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);
}
}
在gcm配置中,要发送消息(服务器端),您需要添加配置:
“数据”=>“声音”=>“默认值”您好,您是如何做到这一点的?你把你的定制声音放在哪里了?在www/folder..@user1027620中,我将声音文件放在[www或app]/res/raw中。我通过在Java代码中更改PushPlugin的本机部分实现了这一点,如上图所示。@AlonAmir。。当应用程序未运行时,如何将收到的通知有效负载保存在sqlite中。@SubheadUpsingh,您不需要对sqlite执行任何操作即可播放推送声音。但是,如果你有理由这样做,我想你可以用Java编写你的sqlite代码(仅适用于应用程序未运行的情况)。这说得很好,但在这种情况下,我如何访问由cordova sqlite插件创建的数据库?如果应用程序既不在后台也不在前台,这将不起作用,正如我在回答中提到的:“起初,我认为我应该在else块中添加这一行,就在extras.putBoolean(“前台”,false)行的正下方,但这只会在应用程序实际位于后台的情况下有所帮助,如果应用程序根本没有运行,则不会有帮助。”
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();
{
...
soundname: 'www/res/raw/yoursoundfile.mp3'
...
}
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);
}
}