0

Using the following code from the Android Developer docs I am unable to get sound working in the API 27 (Android O) simulator. It works on an API 24 device. I also double checked in the notification settings that the notification channel is set to play the default sound.

Here is a project with the example code below that you can try on the simulators: Github.

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
            "Test Channel",
            NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(newIncidentChannel);
}

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Test")
                .setContentText("Text")
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true);

int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());

Update 5/16/18:

I'm using the solution here: https://stackoverflow.com/a/46862503/817886 to use the media play to play sounds when the notification comes in. Not ideal but using this until I can find the proper solution.

Update 5/29/18:

The latest version of Android 8.1.0 has fixed this issue.

nickromano
  • 918
  • 8
  • 16
  • 1
    I had same problem and read almost every answer about this sound problem. Finally I got a notification sound. Problem is that if you have subscribed any channel and than modify/edit this channel, notification sound is not working. Just **delete** installed app and **reinstall**. – TeachMeJava Feb 16 '19 at 23:48

2 Answers2

1

You should be set sound in NotificationChannel not in NotificaitonBuilder

For Example

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);

String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel mChannel = new NotificationChannel(channelId ,
            "Test Channel",
            NotificationManager.IMPORTANCE_DEFAULT)

        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
                context.getString(R.string.app_name),
                NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        mChannel.setDescription(msg);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(sound, attributes); // This is IMPORTANT


        if (mNotificationManager != null)
            mNotificationManager.createNotificationChannel(mChannel);
    }
Nikhil Vadoliya
  • 1,542
  • 10
  • 17
-2

Change your

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Test")
                .setContentText("Text")
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true);

Like this :

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Test")
                .setContentText("Text")
                .setDefaults(Notification.DEFAULT_ALL)
                .setSound(RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
                .setAutoCancel(true);
Koustuv Ganguly
  • 897
  • 7
  • 21
  • Hey Koustuv, thank you for the response but this still isn't working for Android Oreo. The sound does work in lower API levels though. I've updated the sample project if you'd like to test: https://github.com/nickromano/android-notification-channel-test – nickromano May 15 '18 at 19:40