在通知频道上设置声音不会播放我的自定义声音(而是播放另一种声音)

时间:2020-09-02 00:39:33

标签: android android-notifications

收到通知时,它会播放声音,但不是我的自定义声音。这是设置频道声音的代码。我需要更改什么?

 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");
    }
  }

1 个答案:

答案 0 :(得分:1)

好的,我不需要更改代码。我认为Android以某种方式缓存了旧声音。为了解决这个问题,我只是在创建NotificationChannel时将频道ID更改为其他名称,因此Android不会使用缓存的声音文件。