Joshua Augustinus
Joshua Augustinus

Reputation: 1670

Setting sound on notification channel doesn't play my custom sound (but plays another sound instead)

When the notification comes it plays a sound but it's not my custom sound. Here's my code to set the sound of the Channel. What do I need to change?

 private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      Uri ringtone = Uri.parse("android.resource://" +  getApplicationContext().getPackageName()+"/" +R.raw.videocall);

      CharSequence name = getString(R.string.channel_name);
      String description = getString(R.string.channel_description);
      int importance = NotificationManager.IMPORTANCE_HIGH;
      NotificationChannel channel = new NotificationChannel(CustomFirebaseMessagingService.CHANNEL_ID, name, importance);
      channel.setDescription(description);
      AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder()
              .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
     channel.setSound(ringtone,audioAttributes.build());


      // Register the channel with the system; you can't change the importance
      // or other notification behaviors after this
      NotificationManager notificationManager = getSystemService(NotificationManager.class);
      notificationManager.createNotificationChannel(channel);
      Log.d(MainActivity.TAG, "Created notification channel");
    }
  }

Upvotes: 0

Views: 376

Answers (1)

Joshua Augustinus
Joshua Augustinus

Reputation: 1670

OK I didn't need to change the code. I think Android somehow cached the old sound. To fix I simply changed the channel id when creating the NotificationChannel to something else so Android woudn't use the cached sound file.

Upvotes: 2

Related Questions