'Firebase notification message no custom sound on Android 11 (legacy)
Notification message received while the app is in background procudes no sound. It is working on Android 8 and 10 but not in 11. The test app is very simple, it just gets the Firebase token and creates a notification channel.
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final String NOTIFICATION_ID_FCM = "notification_channel_fcm";
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager.getNotificationChannel(NOTIFICATION_ID_FCM) != null) {
return;
}
String name = "FCM Notification";
String description = "Update FCM notification";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_FCM, name, importance);
channel.setDescription(description);
// Sound
Uri notificationSoundUri =
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.fcm_notification);
AudioAttributes notificationSoundUriAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
channel.setSound(notificationSoundUri, notificationSoundUriAttributes);
// Vibration. Seems a bug prevents vibration on Android 8 (api 26) in custom notification channels
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{0, 2000});
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(channel);
}
}
The JSON payload notification (from VB.Net test app using Firebase legacy api):
{
"to": "emdoF49wRnaa0e8B7MguVK:1bGZT5Afy88jAnIviPoj_JmFCh7ScUUxcMO-jJHn1ElyPJ",
"priority": "high",
"notification": {
"title": "Test 20220411 1401",
"body": "Test body",
"sound": "fcm_notification.wav",
"android_channel_id": "notification_channel_fcm"
}
}
Same result using Firebase console to send notification (even enabling sound and specifying channel notification id).
UPDATE: This happens on Samsung A10 with Android 11. Although the application appears in the settings as with sound, the notification does not emit sound. If I set the app's notification to silent...then it plays sound :( and when I go back into the notification settings it shows as with sound (although we have previously set it to silent).
Solution 1:[1]
private fun createNotificationChannel(channelId: String, channelName: String, ): String {
val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
val sound = NotificationChannel(channelId, "Channel Sound", NotificationManager.IMPORTANCE_HIGH)
val soundUri = Uri.parse("android.resource://" + applicationContext.packageName + "/" + R.raw.i_phone_notification)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
when (pref.getString("sound","").toString()) {
"1" -> {
sound.setSound(soundUri, audioAttributes)
}
"4" -> {
sound.setSound( null,null)
}
else-> {
sound.setSound( RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),audioAttributes)
}
}
chan.setShowBadge(true)
chan.lightColor = Color.BLUE
chan.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(chan)
service.createNotificationChannel(sound)
return channelId
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nazirjon Kadirov |