a b分区android8.0,Android O(8.0)Notification Channel使用姿势

本文详细介绍了Android通知渠道和渠道组的使用,包括普通通知、带下载进度的通知以及自定义布局的通知。在Android8.0之后,相同渠道的通知会被合并展示,使用NotificationChannel是必须的,可以通过NotificationChannelGroup进行更精细的管理。同时,文章还展示了如何创建、删除通知渠道和渠道组,以及设置相关属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AAffA0nNPuCLAAAAAElFTkSuQmCC

上面这张图,将具有形同形态,相同表现形式的通知渠道A ,通知渠道B归纳到了同一个渠道组中。

通知渠道的引入可以很方便的管理,和归纳同一种类型的通知Notification.

通知渠道组的引入同样可以方便的管理,归纳同一种类型的通知渠道Channel.

1.首先看一下Android8.0之前的普通Notification的样式以及使用姿势

AAffA0nNPuCLAAAAAElFTkSuQmCC

可以看到是多个通知 ,无论是不是同一个应用的通知,逐个排列下来,占满了屏幕,不太友好

接下来我们来看一下代码

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

起一个线程来模拟 下载进度,间隔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

AAffA0nNPuCLAAAAAElFTkSuQmCC

创建一个布局资源,通过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](http://upload-images.jianshu.io/upload_images/2432544-88f3f7a653560051.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

1. 上图可以看到,相同通知渠道的通知已经被合并,而不是一一全部展开。

2. 如果你的项目TargetSDK在26以上,那么你在使用Notification的时候必须指定一个ChannelId,否则当然会报错

3. 下面是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,那么它的样式跟上图一样,使用姿势是下面这样

1. 这里在渠道组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上通知渠道,渠道组的姿势。

- - -

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值