更新应用程序版本后,通知声音不起作用

问题描述

我的应用程序中有多个通知渠道。 对于每个通道,我为存储在我的应用res/raw文件夹中的文件分配了一个声音Uri。

喜欢

// Getting the sound Uri
int resID = context.getResources().getIdentifier(soundItem.getSoundFileName(),"raw",context.getPackageName());
        if (resID != 0) {
            return Uri.parse("android.resource://" + getContext().getPackageName() + "/" + resID);
        }
 // Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(androidChannelId,channelName,notificationmanager.IMPORTANCE_HIGH);

        // Creating an Audio Attribute
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        notificationChannel.setSound(soundUri,audioAttributes);

这在多个版本中都很好用,但是,在我的应用的最新版本中,我们注意到通知不再播放声音。

在进行挖掘和调试时,我发现声音文件的res ID已更改。 因此,例如,如果我的应用R.raw.tone1的1.2版中的12345678R.raw.tone1,那么现在相同的123456799X。 结果导致系统无法再找到文件

现在我不确定该怎么办?重新创建每个版本的频道?

感谢您的帮助

解决方法

改用声音资源的名称。然后你只需要在下次更新时创建一个新频道(并删除旧频道)。

 // using scope function to make sure resource exists in compile time
        val soundUri = R.raw.tone1.run {
            Uri.parse(
                "${ContentResolver.SCHEME_ANDROID_RESOURCE}://"
                    .plus("${context.packageName}/raw/tone1")
            )
        }