【发布时间】:2019-02-23 07:04:11
【问题描述】:
问题:我的应用需要用户可以自定义的通知。例如。为通知设置不同的声音、标题、文本
问题:我知道通知通道只设置一次并且不能修改,所以我认为我可以只使用变量,但是即使使用变量,一旦我选择了一个声音,它就会保持那个声音并且根本无法改变。我能想到的唯一解决方案是每次更改某些内容时都会创建一个新频道,这看起来很愚蠢。 肯定还有别的办法吗??
此外,在 Android Pie 上,通知声音根本不起作用,我不知道为什么。
Uri soundUri = Uri.parse(notificationSound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(customTextTitle)
.setContentText(customTextBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(soundUri != null){
// Changing Default mode of notification
notificationBuilder.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("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(soundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationBuilder.build());
}
【问题讨论】:
-
多声道应该支持多种声音。根据不同的声音创建不同的频道。
-
谢谢,因为用户可能有这么多不同的声音,我觉得每次一个新频道都会变得非常过分。 (对于可能使用它们并且可能看起来有问题的用户)
-
频道的想法是让用户控制通知。您只能拥有有限数量的通知类型。