Android 8 notifications setSound not working

Ricardo picture Ricardo · Sep 17, 2017 · Viewed 16.4k times · Source

I have the following code but everytime I just hear the default android sound.

        // create  channel
        NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID,
                ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        channel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        channel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        channel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        //channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/" + R.raw.aperturaabductores);

        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();
        channel.setSound(uri,att);

This is my sound pablomonteserin.es/aperturaabductores.wav

Answer

Arnav M. picture Arnav M. · Sep 27, 2017

I tried to see the difference between your sound file and mine. I used Audacity software. Your sound file has sampling rate 22050Hz while the sound files i use are sampled at 44100Hz. So i converted your sound file sampling rate to 44100Hz and used that as notification sound. Now it works.

The problem is with the sound file. May be it's new change in Android O because it's working fine on older Android version.

This is how to resample-enter image description here