1

I am trying to create a notification channel in Android that would play a sound from my Raw folder.

As you can see in below code, I have put this line:

RingtoneManager.getRingtone(a, uri).play();

That allows me to test that Uri is correct, and this is indeed the case.

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

        CharSequence name = a.getString(R.string.channel_name);
        NotificationManager notificationManager = a.getSystemService(NotificationManager.class);

        notificationManager.deleteNotificationChannel(a.getString(R.string.default_notification_channel_id));

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(a.getApplicationContext());

        Uri uri;
        uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + a.getPackageName() + "/raw/" + prefs.getString("pref_ring", "a1"));

        RingtoneManager.getRingtone(a, uri).play();
        //CORRECT SOUND IS PLAYING, URI IS OK

        String description = a.getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(a.getString(R.string.default_notification_channel_id), name, importance);
        channel.setDescription(description);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        channel.setSound(uri, audioAttributes);
        notificationManager.createNotificationChannel(channel);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(a, a.getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("TEST")
                .setContentText("TEST")
                .setAutoCancel(true)
                .setSound(uri)//Just in case, but has no effect
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        notificationManager.notify(0, mBuilder.build());
    }

Unfortunately, ,the notification that is fired always plays default notification ringtone

Any idea what I am doing wrong?

Waza_Be
  • 39,407
  • 49
  • 186
  • 260

0 Answers0