Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Oreo Does not Play Custom Sound for Notification

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

like image 533
Mehroze Yaqoob Avatar asked Apr 27 '18 12:04

Mehroze Yaqoob


People also ask

Why is my custom notification sound not working?

Try these steps: Go to Settings > Sound & Notification > App Notifications. Select the app, and make sure that Notifications are turned on and set to Normal. Make sure that Do Not Disturb is turned off.

Can you get custom notification Sounds on Android?

Open “Settings” on your Android phone. Click on “Apps & Notifications.” Click on “See all Apps” to select the app for which you'd like to customize the notification sound. You should see a list of all the apps that support push notifications on your device.

Why are my notifications not making sound android?

Your phone could be on mute or Do Not Disturb (DND) modes. You need to disable these modes to allow sound notifications. You may have accidentally set incorrect notification settings that prevent you from receiving any notifications.


1 Answers

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());
like image 78
Mehroze Yaqoob Avatar answered Sep 25 '22 13:09

Mehroze Yaqoob