【问题标题】:Get android custom notification to play获取android自定义通知播放
【发布时间】:2019-01-30 09:45:54
【问题描述】:

美好的一天,

我的项目中有以下代码来执行通知,因此通知工作正常,但我想添加以支持 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());
        }
    }

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

GET CUSTOM NOTIFICATION FOR OREO

GET URI FROM RAW FILE

【问题讨论】:

    标签: android uri custom-notification


    【解决方案1】:

    不确定 URI 是否错误,但这肯定有问题。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多