我有以下有效负载推送通知,从我的服务器发送。该通知适用于所有版本,但声音仅在Android Oreo上不起作用,其他Android版本工作正常。
{
"to" : "d4DLcrilLbs...",
"notification" : {
"body" : "This is an FCM notification message!",
"title" : "FCM Message",
"sound" : "new_sound.wav"
}
}
在oreo中你必须用通道设置声音
请用此更改您的代码
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + this.getPackageName() + "/raw/notification");
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel channel = new NotificationChannel("MyNotification","MyNotification", NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(alarmSound,attributes);
NotificationManager mgr =getSystemService(NotificationManager.class);
mgr.createNotificationChannel(channel);
}
它将适用于您的应用程序在后台/前台