Android O-通知频道和NotificationCompat [英] Android O - Notification Channels and NotificationCompat
问题描述
我无法改变这种感觉:再次,Android开发人员想出了一些新东西,使每个人都不知道他们如何使用该功能.
I cannot change this feeling: again, the Android developers came up with something new and leave everybody in the dark about how they would think the feature is used.
我正在谈论Android O中的通知频道.
I am talking about Notification Channels in Android O.
多年来,我一直在使用兼容性支持库来避免处理特定的平台详细信息.即:NotificationCompat
.
For years I have been using the compatibility support libraries to avoid dealing with specific platform details. Namely: NotificationCompat
.
现在,Builder
要求我提供一个通知频道ID,这很不错,但是完全让我独自创建了一个这样的频道.我找不到创建频道的任何兼容支持.我也找不到在正确的位置创建它们的合理方法.
Now, the Builder
requires me to supply a notification channel id, which is nice, but completely leaves me alone with creating such a channel. I cannot find any compat support for creating channels. Nor can I find a reasonable way to create them at the right point.
文档只是简单地指出应在某处"完成,在发出通知时可能不行".但是我到底该怎么办?我讨厌为简单的任务编写特定于版本的内容-这就是为什么我使用compat库.
The docs simply state that it should be done "somewhere" and "probably not when issuing a notification". But what exactly am I supposed to do? I hate writing version specific stuff for simple tasks - that's why I use the compat libraries.
有人对如何处理有建议吗?每当我希望显示通知时,每次创建都昂贵"吗?
Does anybody have a suggestion on how to handle it? Is it "expensive" to do the creating each and every time when I want a notification to be displayed?
推荐答案
这是我在Android O上生成通知并保持向后兼容性的解决方案:
This is my solution to generate notifications on Android O and maintain backward compatibility:
String idChannel = "my_channel_01";
Intent mainIntent;
mainIntent = new Intent(context, LauncherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, null);
builder.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(getNotificationIcon())
.setContentIntent(pendingIntent)
.setContentText(context.getString(R.string.alarm_notification) + ManagementDate.getIstance().hourFormat.format(getAlarm(context, 0)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(idChannel, context.getString(R.string.app_name), importance);
// Configure the notification channel.
mChannel.setDescription(context.getString(R.string.alarm_notification));
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
} else {
builder.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.transparent))
.setVibrate(new long[]{100, 250})
.setLights(Color.YELLOW, 500, 5000)
.setAutoCancel(true);
}
mNotificationManager.notify(1, builder.build());
这篇关于Android O-通知频道和NotificationCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!