12

I am trying to add a custom sound to notification for API > 26. Below is the code

NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);

The problem here is that it, plays default piano sound of device rather than playing beep sound from assets. I am not allowed to use ringtone manager but common sense stats that notification sound should be that which is specified rather than default.

It works fine for API <= 26

Mehroze Yaqoob
  • 1,011
  • 1
  • 12
  • 29

1 Answers1

14

Finally I managed to find a solution on my own. Below is the code

NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            if(notificationSoundUri != null){
                // Changing Default mode of notification
                notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);

                // Creating an Audio Attribute
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build();

                // Creating Channel
                NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(notificationSoundUri,audioAttributes);
                mNotificationManager.createNotificationChannel(notificationChannel);
            }
}
mNotificationManager.notify(0, notificationCompatBuilder.build());
Mehroze Yaqoob
  • 1,011
  • 1
  • 12
  • 29
  • Yes, but once the channel is created the only way to change its sound or vibration is through the android settings – from56 May 01 '18 at 10:16
  • That is up to user who wants to change or mute sound but pro-grammatically I was require to set custom sound – Mehroze Yaqoob May 01 '18 at 10:43
  • Thank you! Setting the sound on the notification channel worked for me as well in Oreo. I'm getting my sounds from android resources. – efeder May 02 '18 at 16:28
  • OK, it means that you have to `createNotificationChannel` with `audioAttributes` then it would work. – John Jang May 22 '18 at 12:37
  • where have u defined notificationSoundUri – Panache May 23 '18 at 07:16
  • Is there any way to Inside app user can change the sound setting (disable or enable) considering notification channel functionality? – samit Jul 24 '18 at 06:13
  • If you need to make changes to sound, simply delete the channel and recreate it – Lucem Sep 14 '18 at 14:29
  • @LionHeart In my android Pie(9) doesn't work. If you know why then can you please suggest me any solution. – Ali Dec 28 '18 at 07:12
  • @Ali - were you able to find a solution for Android Pie(9). This code works for me on Android 8 only – Tulika May 16 '19 at 01:06