Android O(8.0)Notification Chann
2018-01-29 本文已影响936人
蓅哖伊人为谁笑
NotificationGroup通知渠道组示意图.png
上面这张图,将具有形同形态,相同表现形式的通知渠道A ,通知渠道B归纳到了同一个渠道组中。
- 通知渠道的引入可以很方便的管理,和归纳同一种类型的通知Notification.
- 通知渠道组的引入同样可以方便的管理,归纳同一种类型的通知渠道Channel.
1.首先看一下Android8.0之前的普通Notification的样式以及使用姿势
普通多个notification.png- 可以看到是多个通知 ,无论是不是同一个应用的通知,逐个排列下来,占满了屏幕,不太友好
- 接下来我们来看一下代码
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle("你有一条新的消息")
.setContentText("this is normal notification style")
.setTicker("notification ticker")
.setPriority(1000)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setNumber(3)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingResult)
.setOngoing(true);
Notification notification = mBuilder.build();
getNotificationManager().notify(notifyId, notification);
2.再来看一下带下载进度的通知栏Notification
带下载进度Notification.png- 起一个线程来模拟 下载进度,间隔1秒更新一下 通知进度。
final NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setOngoing(true)
.setVibrate(new long[]{0})
.setSound(null)
.setTicker("notification ticker")
.setDefaults(NotificationCompat.FLAG_LOCAL_ONLY)
.setSmallIcon(android.R.drawable.stat_notify_chat);
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr += 5) {
mBuilder.setProgress(100, incr, false);
mBuilder.setSound(null);
mNotifyManager.notify(0, mBuilder.build());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
mBuilder.setContentText("Download complete").setProgress(0, 0, false);
mBuilder.setAutoCancel(true);
mBuilder.setOngoing(false);
mNotifyManager.notify(0, mBuilder.build());
}
}
).start();
3.再来看一下自定义布局的Notification
自定义布局Notification.png- 创建一个布局资源,通过RemoteViews来 渲染,同时也绑定了点击事件
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remote_custom_notification);
remoteViews.setTextViewText(R.id.title, "Custom Notification---title");
remoteViews.setTextViewText(R.id.content, "Custom Notification---content");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_NOTIFY, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContent(remoteViews)
.setTicker("ticker")
.setContentIntent(pendingIntent).build();
getNotificationManager().notify(notifyId, notification);
以上【1】【2】【3】就是常用的通知栏Notification的样式和使用姿势
4.下面来看一下Android 8.0上通知渠道NotificationChannel 的应用
NotificationChannel.png- 上图可以看到,相同通知渠道的通知已经被合并,而不是一一全部展开。
- 如果你的项目TargetSDK在26以上,那么你在使用Notification的时候必须指定一个ChannelId,否则当然会报错
- 下面是Android 8.0上通知渠道NotificationChannel 的使用代码段,注意点需要传入CHANNEL_ID(随意指定),CHANNEL_NAME(随意指定)
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setBypassDnd(true); //设置绕过免打扰模式
channel.canBypassDnd(); //检测是否绕过免打扰模式
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知
channel.setDescription("description of this notification");
channel.setLightColor(Color.GREEN);
channel.setName("name of this notification");
channel.setShowBadge(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
channel.enableVibration(true);
getNotificationManager().createNotificationChannel(channel);
5.如果要使用通知渠道组NotificationChannelGroup,那么它的样式跟上图一样,使用姿势是下面这样
- 这里在渠道组NotificationChannelGroup上绑定了两个通知渠道NotificationChannel ,每个渠道下各有一个通知Notification.
getNotificationManager().createNotificationChannelGroup(new NotificationChannelGroup(GROUP_ID, "GROUP_CHANNEL"));
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(GROUP_ID);
channel.setShowBadge(true);
channel.setLightColor(Color.RED);
channel.enableLights(true);
getNotificationManager().createNotificationChannel(channel);
NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_2, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel2.setGroup(GROUP_ID);
channel2.setShowBadge(true);
channel2.setLightColor(Color.RED);
channel2.enableLights(true);
getNotificationManager().createNotificationChannel(channel2);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
PendingIntent pendingResult = PendingIntent.getActivity(this, REQUEST_CODE_NOTIFY, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle("notification title_9")
.setContentText("notification content_9")
.setPriority(1000)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setNumber(3)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setContentIntent(pendingResult)
.setOngoing(true);
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID_2)
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle("notification title_10")
.setContentText("notification content_10")
.setPriority(1000)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setNumber(15)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingResult)
.setOngoing(true);
getNotificationManager().notify(9, mBuilder.build());
getNotificationManager().notify(10, mBuilder1.build());
6.通知渠道的管理
1.删除渠道
getNotificationManager().deleteNotificationChannel(CHANNEL_ID);
2.删除渠道组
getNotificationManager().deleteNotificationChannelGroup(GROUP_ID);
以上便是老版本上使用Notification 和 Android 8.0上通知渠道,渠道组的姿势。