介绍
随着Android 8.0的发布,google更新了Notification内容,新增了通知渠道这个概念,用来对通知进行分类管理,以提高用户体验。关于Android 8.0中NotificationChannel的管理在《设置》-》《应用管理》-》《具体应用》-》《通知》或者 《设置》-》《通知》(具体应手机不同可能略有差异)就可以看到应用已经设置的通知渠道,这里可以对不同渠道通知管理。
创建NotificationChannl
通过NotificationManager的createNotificationChannel方法来创建NotificationChannel:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public void createNotificationChannel(String id, String name, int importance, String desc) {
if (mNotificationManager.getNotificationChannel(id) != null) return;
NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationChannel.setShowBadge(true);
notificationChannel.setBypassDnd(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400});
notificationChannel.setDescription(desc);
mNotificationManager.createNotificationChannel(notificationChannel);
}
NotificationChannel 的方法列表:
- getId() — 获取 ChannleId
- enableLights() — 开启指示灯,如果设备有的话。
- setLightColor() — 设置指示灯颜色
- enableVibration() — 开启震动
- setVibrationPattern() — 设置震动频率
- setImportance() — 设置频道重要性
- getImportance() — 获取频道重要性
- setSound() — 设置声音
- getSound() — 获取声音
- setGroup() — 设置 ChannleGroup
- getGroup() — 得到 ChannleGroup
- setBypassDnd() — 设置绕过免打扰模式
- canBypassDnd() — 检测是否绕过免打扰模式
- getName() — 获取名称
- setLockScreenVisibility() — 设置是否应在锁定屏幕上显示此频道的通知
- getLockscreenVisibility() — 检测是否应在锁定屏幕上显示此频道的通知
- setShowBadge() 设置是否显示角标
- canShowBadge() — 检测是否显示角标
以上属性表示了隶属这个渠道的通知显示效果。
setImportance 重要程度
越高,提示权限就越高,最高的支持发出声音&悬浮通知。1
2
3
4
5
6
7public static final int IMPORTANCE_DEFAULT = 3;
public static final int IMPORTANCE_HIGH = 4;
public static final int IMPORTANCE_LOW = 2;
public static final int IMPORTANCE_MAX = 5;
public static final int IMPORTANCE_MIN = 1;
public static final int IMPORTANCE_NONE = 0;
public static final int IMPORTANCE_UNSPECIFIED = -1000;
删除 NotificationChannel
通过 NotificationManager 的 deleteNotificationChannel 方法来删除 NotificationChannel。1
mNotificationManager.deleteNotificationChannel(chatChannelId);
发出通知
只需要设置一个 ChannelId 即可发布到对应的 Channel 上,需要注意的是 NotificationChannel 一定要先创建才行。1
2
3
4
5
6
7
8
9
10
11
12
13
14Notification.Builder builder = new Notification.Builder(this, chatChannelId);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Gavin")
.setContentText("Today released Android 8.0 version of its name is Oreo")
.setAutoCancel(true);
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
mNotificationManager.notify((int) System.currentTimeMillis(), builder.build());
角标管理
首先要开启允许使用通知圆点,这个是用户可以取消的,如果要显示一定要代码中保证是开启状态。
NotificationChannel 开启角标
1
notificationChannel.setShowBadge(true);
Notification 设置角标样式
1
new Notification.Builder(this, chatChannelId).setBadgeIconType(BADGE_ICON_SMALL)
Notification 设置角标计数
1
new Notification.Builder(this, chatChannelId).setNumber(1)
跳转设置
1 | Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); |
使用 NotificationChannleGroup
如果你的通知渠道比较多,那么久可以考虑使用 NotificationChannleGroup 来管理一下 。
创建 NotificationChannleGroup
和创建 NotificationChannle 类似1
mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
NotificationChannle 绑定 groupId
1 | notificationChannel.setGroup(groupId); |
删除 NotificationChannleGroup
可以批量删除该 Group 下的所有 Channel
1 | mNotificationManager.deleteNotificationChannelGroup(groupId2); |
参考地址:
[1]. NotificationChannl运用 http://gavinliu.cn/2017/08/22/Android-%E5%90%83%E5%A5%A5%E5%88%A9%E5%A5%A5%E7%B3%BB%E5%88%97-1-Notification/