0

Good Day,

I have the following code in my project to do notifications, so the notifications work fine, but the custom sound i want to add to support android 8 is not working ... it just uses the phone's notification sound.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        Uri mysound = null;
        if(sound.equalsIgnoreCase("visitor_long"))
        {
            mysound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName()+  "/res" + "/raw/" + sound + ".wav");
        }
        else{
            mysound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/raw/" + sound);
        }
        AudioAttributes attributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build();

        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, contentTitle, NotificationManager.IMPORTANCE_HIGH);

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

        if (notificationManager != null)
            notificationManager.createNotificationChannel(mChannel);

   }

    if(notificationManager != null ) {
        if(message != null) {
            int messageHash = message.hashCode();
            notificationManager.notify(messageHash, notificationBuilder.build());
        }
    }

i have used the following links, but it just wont work:

GET CUSTOM NOTIFICATION FOR OREO

GET URI FROM RAW FILE

Arrie
  • 1,327
  • 5
  • 18
  • 39

1 Answers1

1

Not sure if the URI is wrong, but this would definitly be buggy.

The resource IDs might change from one build to the other, and so would the URI. But since you cannot change the sound URI after a channel is created, an update to your app will lead to the custom sound not being played again.

You could use the way mentioned here to create the URI: https://stackoverflow.com/a/38340580/153721

Konsumierer
  • 1,295
  • 1
  • 15
  • 33