android通知api,android – API 26上的通知声音

博客内容讲述了开发者遇到的一个问题,即在API26及以上版本的设备上,尽管尝试通过NotificationChannel设置自定义的mp3声音,但通知仍然使用默认声音。代码展示了创建Notification和NotificationChannel的详细过程,包括设置音频属性和资源URI,但问题仍未解决。
摘要由CSDN通过智能技术生成

我有一个我在通知中使用的自定义mp3声音.它适用于API 26以下的所有设备.我也尝试在Notification Channel上设置声音,但仍无法正常工作.它播放默认声音.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)

.setAutoCancel(true)

.setSmallIcon(R.drawable.icon_push)

.setColor(ContextCompat.getColor(this, R.color.green))

.setContentTitle(title)

.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))

.setDefaults(Notification.DEFAULT_VIBRATE)

.setStyle(new NotificationCompat.BigTextStyle().bigText(message))

.setContentText(message);

Notification notification = builder.build();

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

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

NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);

AudioAttributes audioAttributes = new AudioAttributes.Builder()

.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)

.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)

.build();

channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes);

notificationManager.createNotificationChannel(channel);

}

notificationManager.notify(1, notification);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值