![](/img/trans.png)
[英]No notification sound and vibrate and lights when sending notification from firebase in android background
[英]No notification sound when sending notification from firebase in android
我正在将推送通知从 firebase 发送到我的 Android 应用程序。 但是当我的应用程序在后台 firebase onMessageReceived 方法不被调用而是 firebase 向系统发送通知以在系统托盘中显示通知。 通知出现在系统托盘中,但没有通知声音,即使我在系统设置中允许我的应用程序发出通知声音。
当从 firebase 收到通知时,我可以做些什么来播放通知声音。
这就是我将通知从firebase发送到我的应用程序Blogpost 链接的方式。
在通知的通知负载中有一个声音键。
从官方文档来看,它的用法是:
表示设备收到通知时播放的声音。 支持应用程序中捆绑的声音资源的默认或文件名。 声音文件必须位于 /res/raw/ 中。
例如:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon",
"sound" : "mySound"
}
}
如果你想使用设备的默认声音,你应该使用: "sound": "default"
。
有关有效负载中所有可能的密钥,请参阅此链接: https : //firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
对于那些不知道 firebase 的人来说,当应用程序在后台时,它会以不同的方式处理通知。 在这种情况下,不会调用 onMessageReceived 函数。
当您的应用程序在后台时,Android 会将通知消息定向到系统托盘。 默认情况下,用户点击通知会打开应用启动器。 这包括同时包含通知和数据负载的消息。 在这些情况下,通知会传送到设备的系统托盘,数据有效负载会传送到启动器 Activity 的意图的附加内容中。
试试这个
{
"to" : "DEVICE-TOKEN",
"notification" : {
"body" : "NOTIFICATION BODY",
"title" : "NOTIFICATION TITILE",
"sound" : "default"
}
}
@note 自定义通知声音:-> "sound" : "MyCustomeSound.wav"
仅当应用程序处于前台或通知负载仅包含数据类型时才会触发onMessageReceived
方法。
对于下游消息传递,FCM 提供两种类型的有效负载:通知和数据。
对于通知类型,FCM 会代表客户端应用程序自动向最终用户设备显示消息。 通知具有一组预定义的用户可见键。
对于数据类型,客户端应用程序负责处理数据消息。 数据消息只有自定义键值对。当您希望FCM代表您的客户端应用程序处理显示通知时,请使用通知。 当您希望您的应用程序在您的 Android 客户端应用程序上处理显示或处理消息时,或者如果您希望在存在直接 FCM 连接时向 iOS 设备发送消息,请使用数据消息。
进一步向下看文档
接收同时包含通知和数据有效载荷的消息时的应用行为取决于应用是在后台还是在前台——本质上,它在接收时是否处于活动状态。
在后台时,应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载。
在前台时,您的应用程序会收到一个消息对象,其中的两个有效负载都可用。
如果您使用 Firebase 控制台发送通知,则负载将始终包含通知类型。 您必须使用 Firebase API 发送仅包含通知负载中数据类型的通知。 这样,您的应用程序始终会在收到新通知时收到通知,并且该应用程序可以处理通知负载。
如果您想使用常规方法在应用程序处于后台时播放通知声音,则需要将声音参数添加到通知负载中。
使用 HTTP v1 API 则不同
示例:
{
"message":{
"topic":"news",
"notification":{
"body":"Very good news",
"title":"Good news"
},
"android":{
"notification":{
"body":"Very good news",
"title":"Good news",
"sound":"default"
}
}
}
}
我也遇到了必须发出声音的通知问题,当应用程序在前台时一切正常,但是当应用程序在后台时,声音就没有出来。
通知由服务器通过 FCM 发送,即服务器挂载通知的 JSON 并将其发送给 FCM,然后由 FCM 将通知发送给应用程序。 即使我放了声音标签,声音也不会在背景中出现。
即使把声音标签它也不起作用。
经过如此多的搜索,我在 github 论坛上找到了解决方案。 然后我注意到我的情况有两个问题:
1 - 缺少发送 channel_id 标签,这对于在 API 级别 26+ 中工作很重要
2 - 在 Android 应用程序中,对于这种直接从服务器发送通知的特定情况,我必须提前配置通道 ID,因此在我的主 Activity 中,我必须配置通道,以便 Android 知道何时该做什么通知到了。
在服务器发送的 JSON 中:
{
"title": string,
"body": string,
"icon": string,
"color": string,
"sound": mysound,
"channel_id": videocall,
//More stuff here ...
}
在您的主要活动中:
@Background
void createChannel(){
Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.app_note_call);
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel("videocall", "VIDEO CALL", NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription("VIDEO CALL");
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
mChannel.setSound(sound, audioAttributes);
NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(mChannel);
}
}
这终于解决了我的问题,我希望它可以帮助人们不要像我一样浪费 2 天。 我不知道我在代码中输入的所有内容是否都需要,但就是这样。 我也没有找到 github 论坛链接来证明答案,因为我所做的与发布在那里的相同。
这样做
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//codes..,.,,
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(sound);
}
试试这个....
public void buildPushNotification(Context ctx, String content, int icon, CharSequence text, boolean silent) {
Intent intent = new Intent(ctx, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
Bitmap bm = BitmapFactory.decodeResource(ctx.getResources(), //large drawable);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(icon)
.setLargeIcon(bm)
.setContentTitle(content)
.setContentText(text)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if(!silent)
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410, notificationBuilder.build());
}
并在 onMessageReceived 中调用它
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("Msg", "Message received [" + remoteMessage.getNotification().getBody() + "]");
buildPushNotification(/*your param*/);
}
或者按照KongJing ,他说的也是正确的,但是您可以使用 Firebase 控制台。
即使我从 firebase 控制台发送通知声音,我也可以播放通知声音。 要做到这一点,您只需要在预先选项中添加值为“default”的键“sound”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.