Notification Sound On Api 26
I have a custom mp3 sound that I use on my notifications. It works fine on all devices below API 26. I tried to set the sound on Notification Channel also, but still no work. It pl
Solution 1:
You may have created the channel originally with default sound. Once channel is created it cannot be changed. You need to either reinstall the app or create channel with new channel ID.
Solution 2:
I used RingtoneManager, and it is work for me. Try thius code:
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
Intentintent=newIntent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
PendingIntentpendingIntent= PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Notification title");
builder.setContentText("Notification message.");
builder.setSubText("Url link.");
try {
Urinotification= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_ringtone);
Ringtoner= RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
NotificationManagernotificationManager= (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
Solution 3:
In Oreo you need to create a channel for that. https://developer.android.com/reference/android/app/NotificationChannel.html
Solution 4:
The default sound overrides any sound.
You need to put this in your code:
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
Reference:
Solution 5:
In addition to the Faxriddin's answer you can determine when you should to turn off the notification sound by checking the importance of the notification channel
and are enabled notifications
parameters.
NotificationChannelchannel= notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
if(notificationManager.areNotificationsEnabled() && channel.getImportance() != NotificationManager.IMPORTANCE_NONE) {
try {
Ringtoner= RingtoneManager.getRingtone(ctx, soundUri);
r.play();
} catch (Exception e) { }
}
Post a Comment for "Notification Sound On Api 26"