【问题标题】:Android Oreo Does not Play Custom Sound for NotificationAndroid Oreo 不为通知播放自定义声音
【发布时间】:2018-04-27 12:37:43
【问题描述】:

我正在尝试为 API > 26 的通知添加自定义声音。下面是代码

NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);

这里的问题是它播放设备的默认钢琴声音,而不是从资产播放哔声。我不允许使用铃声管理器,但根据常识统计,通知声音应该是指定的而不是默认的。

它适用于 API

【问题讨论】:

  • 对26以下的API有效吗?
  • 亲爱的 Pankaj,它正在使用铃声管理器,由于要求,我不想使用它。
  • 检查this
  • 不,这也没有帮助。我的操作系统版本是 8.1.0

标签: android android-notifications android-8.0-oreo android-8.1-oreo


【解决方案1】:

最后我设法自己找到了解决方案。下面是代码

NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            if(notificationSoundUri != null){
                // Changing Default mode of notification
                notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);

                // Creating an Audio Attribute
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build();

                // Creating Channel
                NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(notificationSoundUri,audioAttributes);
                mNotificationManager.createNotificationChannel(notificationChannel);
            }
}
mNotificationManager.notify(0, notificationCompatBuilder.build());

【讨论】:

  • 是的,但是一旦创建了频道,更改其声音或振动的唯一方法就是通过 android 设置
  • 这取决于想要更改或静音的用户,但在程序上我需要设置自定义声音
  • 谢谢!在 Oreo 中设置通知频道的声音也适用于我。我正在从 android 资源中获取我的声音。
  • 好的,这意味着你必须createNotificationChannelaudioAttributes 才能工作。
  • 你在哪里定义了 notificationSoundUri
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
相关资源
最近更新 更多