Zor*_*did 22 java android-notifications android-support-library android-8.0-oreo
我无法改变这种感觉:再次,Android开发人员提出了一些新的东西,让所有人都不知道他们会如何看待这个功能.
我在谈论Android O中的通知频道.
多年来,我一直在使用兼容性支持库来避免处理特定的平台细节.即:NotificationCompat
.
现在,Builder
要求我提供一个通知通道ID,这很好,但完全不用我创建这样一个通道.我找不到任何compat支持创建频道.我也找不到合适的方法来在正确的点上创建它们.
文档只是声明它应该"在某处"和"在发出通知时可能不会".但究竟我应该做什么?我讨厌为简单的任务编写特定版本的东西 - 这就是我使用compat库的原因.
有没有人建议如何处理它?当我想要显示通知时,每次创建是否"昂贵"?
Ale*_*Pad 12
这是我在Android O上生成通知并保持向后兼容性的解决方案:
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());
Run Code Online (Sandbox Code Playgroud)
它没有你想象的那么贵!您需要做的就是创建一个通知通道并将其绑定到通知。
您可以通过两种方式解决此问题,但是对于这两种方式,您都需要创建具有特定通道 ID 的通知通道。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
mChannel.enableLights(true);
mNotificationManager.createNotificationChannel(mChannel);
Run Code Online (Sandbox Code Playgroud)
第一种方法是在构造函数中设置通知通道:
Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title");
mNotificationManager.notify("your_notification_id", notification);
Run Code Online (Sandbox Code Playgroud)
第二种方法是通过 Notificiation.Builder.setChannelId() 设置通道
Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title").
setChannelId(id);
mNotificationManager.notify("your_notification_id", notification);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
归档时间: |
|
查看次数: |
13328 次 |
最近记录: |