0

再会,

我的项目中有以下代码来执行通知,因此通知工作正常,但我想添加以支持 android 8 的自定义声音不起作用......它只是使用手机的通知声音。

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

        Uri mysound = null;
        if(sound.equalsIgnoreCase("visitor_long"))
        {
            mysound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName()+  "/res" + "/raw/" + sound + ".wav");
        }
        else{
            mysound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/raw/" + sound);
        }
        AudioAttributes attributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build();

        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, contentTitle, NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        mChannel.setDescription(message);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(mysound, attributes); // This is IMPORTANT

        if (notificationManager != null)
            notificationManager.createNotificationChannel(mChannel);

   }

    if(notificationManager != null ) {
        if(message != null) {
            int messageHash = message.hashCode();
            notificationManager.notify(messageHash, notificationBuilder.build());
        }
    }

我使用了以下链接,但它不起作用:

获取奥利奥的定制通知

从原始文件获取 URI

4

1 回答 1

1

不确定 URI 是否错误,但这肯定是错误的。

资源 ID 可能会从一个构建更改为另一个构建,URI 也会如此。但是由于在创建频道后您无法更改声音 URI,因此对您的应用程序的更新将导致自定义声音不再播放。

您可以使用此处提到的方式创建 URI: https ://stackoverflow.com/a/38340580/153721

于 2019-08-16T11:06:36.347 回答