Why is Galaxy S21 not doing custom notification sounds

228 Views Asked by At

I have an app that can emit custom notification sounds triggered by calendar events. This worked perfectly well on my old phone, but on my new Galaxy S21, it emits the system default notification sound instead of the custom notification sound. I tried it on the emulator with an AVD running Android 11, which is the version that the S21 claims to be running, and it works correctly. The custom sound exists on the S21 and is playable using the music player.

Android Notification sound defaulting back instead of playing custom sound is a very old question reporting a similar problem and the answer there suggested rebooting the phone, which I tried and it didn't help.

The code which emits the notification looks like this:-

private void emitNotification(String smallText, String bigText, String path) {
    RemoteViews layout = new RemoteViews(
        "uk.co.yahoo.p1rpp.calendartrigger",
        R.layout.notification);
    layout.setTextViewText(R.id.notificationtext, bigText);
    DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
    layout.setTextViewText(R.id.notificationtime,
                           df.format(System.currentTimeMillis()));
    Notification.Builder NBuilder
        = new Notification.Builder(this)
        .setSmallIcon(R.drawable.notif_icon)
        .setContentTitle(smallText)
        .setContent(layout);
    if ((path != null) && !path.isEmpty())
    {
        Uri uri = new Uri.Builder().path(path).build();
        AudioAttributes.Builder ABuilder
            = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
            .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION);
        NBuilder.setSound(uri, ABuilder.build());
    }
    // Show notification
    NotificationManager notifManager = (NotificationManager)
        getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.notify(notifyId++, NBuilder.build());
}

The entire source is available on GitHub at rparkins999/CalendarTrigger branch fix3.3

I have included a built APK in the git tree if anyone would like to try it on another phone. You can also build from the sources: you will need a ../Keys/Keystore.properties file relative to the project root directory since I'm not publishing my private signing key.

To demonstrate a custom notification sound, run the program, give it all the permissions it wants, touch 'NEW EVENT CLASS', give the class a name, and touch Create: you should then see a screen consisting of buttons that invoke activities to define the characteristics of the class.

Touch 'Event starts action(s) for class ...' and you should see a screen offering various actions that the program can take (not all of which work). Touch 'Show a notification', then 'Play a sound', then 'Browse for a sound file': you should then see a file browser.

When you choose a file it will take you back to the previous screen, but 'Browse for a sound file' will have been replaced by the path to the sound file. Touch the back button to return to the screen of buttons, and then touch 'Immediate event of class ...'. It should play the sound.

On the emulator it does, but on the S21 it plays the default sound instead.

As set up, the class you created will emit a notification on every calendar event. You can stop that by deleting the class or setting some conditions for a calendar event to be in the class: there are buttons for both of these actions on the class definition screen.

I don't know whether this problem is a bug in the S21, or caused by some obscure Setting that I haven't been able to find. Any help would be appreciated, especially information as to whether the app does or doesn't work correctly on other phones running Android 11. Of course, a fix or a workaround would be better still!

I just installed the latest S21 update. Now the problem is worse! I don't get any sound at all from my notifications with custom sounds.

1

There are 1 best solutions below

0
Richard Parkins On

The problem in this particular case was the audio format in the file (.m4a). The music player on the S21 can play it, and the notification sound player in my old phone could play it, but the notification player on the S21 apparently couldn't play it.

I used FFmpeg on my PC to convert from .m4a to .mp3, which the S21 notification player can play, and it now works.

So it isn't safe to assume that if the phone can play a sound format with its music player, the Notification logic can also handle it.