Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 plays the default sound.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)             .setAutoCancel(true)             .setSmallIcon(R.drawable.icon_push)             .setColor(ContextCompat.getColor(this, R.color.green))             .setContentTitle(title)             .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))             .setDefaults(Notification.DEFAULT_VIBRATE)             .setStyle(new NotificationCompat.BigTextStyle().bigText(message))             .setContentText(message);         Notification notification = builder.build();         NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {             NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);             AudioAttributes audioAttributes = new AudioAttributes.Builder()                     .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)                     .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)                     .build();             channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes);             notificationManager.createNotificationChannel(channel);         }         notificationManager.notify(1, notification); 
like image 566
Rodrigo Manguinho Avatar asked Sep 03 '17 01:09

Rodrigo Manguinho


People also ask

How do I get my notification sound?

Open Settings. Tap Sounds & vibration. Tap Notification sound and vibration. You should now see a list of available noises.

What notification sounds mean?

One of the major advantages of using Android is that you can tweak almost everything about it, including the device's default global notification sounds. These are the sounds you hear when you receive a text message on your phone or an alert from an app.


1 Answers

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.

like image 119
Paweł Nadolski Avatar answered Sep 19 '22 19:09

Paweł Nadolski