2

I'm trying to implement a custom notification sound in my application. I have written the following code, but the application plays only default sound and not the custom sound i've added in raw folder. Upon receiving the notification, the logs doesn't even throw any error or exception as to why it isn't playing the custom sound. I tried searching online and tried following different approaches but to no avail. Please let me know where am i going wrong.

Edit: Can someone post the code for it, i cant seem to find anything that works

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.notify);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("MyCuS Notification", "My Notification", NotificationManager.IMPORTANCE_HIGH);
        NotificationManager manager = getSystemService(NotificationManager.class);
        AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
        channel.setSound(Uri.parse("android.resources://" + getPackageName() + "/" + R.raw.bg_reminder_alarm),audioAttributes.build());
        manager.createNotificationChannel(channel);
    }

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "MyCuS Notification");
            builder.setContentTitle("MyTitle");
            builder.setContentText("TESTING");
            builder.setSmallIcon(R.drawable.ic_launcher_background);
            builder.setAutoCancel(true);
            builder.setSound(Uri.parse("android.resources://" + getPackageName() + "/" + R.raw.bg_reminder_alarm));
            NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
            managerCompat.notify(1, builder.build());
        }
    });
}

Edit 2: I tried deleting existing channel and sending notification to create new channel, when newly created the description of the channel changes after sending second notification, it is as if the channel is overridden or deleted and new default channel is created.

enter image description here

Aagam Shah
  • 125
  • 3
  • 11

1 Answers1

2

Since Android Oreo / 8 the Notificationsound is coming from the Channel and can only be set the first time you add the channel via your channel.setSound(). If you want to change it later on you need to delete the channel and then re-add it to the system. The user will be warned about that behaviour though (App deleted channels X amount of times). https://developer.android.com/guide/topics/ui/notifiers/notifications#ManageChannels

If you want to have a customsound each and every time, you need a ForegroundService without a channelsound for it's foreground notification (setSound(null)) and then use the MediaPlayer on the Notificationstream to play the custom sound.

  • So the sound will only play the first time from the channel, next time it will again play the default sound? – Aagam Shah Feb 18 '22 at 04:55
  • It will always play the sound you set in the channel, but only the one you set the first time you created the channel, unless you delete the channel and add it again. This is to ensure that the user can change the channel sound (and all the other settings) via the Android settings. So there really is no need or benefit to add the channel every time you want to sent a push, having it run once on app install is sufficient. – seaforester Feb 21 '22 at 09:54
  • I tried adding the custom sound, yet the notification plays the default sound. I don't know what is going wrong here. – Aagam Shah Feb 21 '22 at 10:36
  • Make sure you delete the previous channel notificationManager.deleteNotificationChannel("MyCuS Notification"); or add a completly new channel, else it will keep using your old default sound. Then on the new creation: channel.setSound(null, null); – seaforester Feb 21 '22 at 13:38
  • I even did that, check out the second edit, somehow the channel is overridden or destroyed and new default channel is created later – Aagam Shah Feb 21 '22 at 14:18
  • Add this to your manifest: And if that doesn't work out, maybe you specify it in your payload, check: https://stackoverflow.com/a/69114548/5796624 – seaforester Feb 23 '22 at 08:25