android 8.0的imei简书,Android 8.0通知栏渠道,渠道组的适配和使用

本文介绍了Android 8.0开始引入的通知渠道和渠道组概念,用于精细化通知管理。通过示例代码展示了如何创建和划分通知渠道及渠道组,并给出了如何在应用中发送特定渠道的通知。文章还提到了微信和QQ等应用的适配情况,以及爱奇艺的详细划分作为参考。最后,提供了创建渠道组和发送通知的步骤,帮助开发者实现类似爱奇艺的通知管理功能。
摘要由CSDN通过智能技术生成

Android 8.0通知栏渠道,渠道组的适配和使用

推广一下 博客

android 8.0开始引入了渠道组的概念,app可以对通知类型进行细分,比如划分为:广告消息,私聊消息,群聊消息等。

但其实到目前为止,查看了下微信,qq基本是没有做这样的适配的。,反倒是爱奇艺划分的比较详细,不仅划分了通知渠道,更划分了渠道组。

本文分别介绍如何划分渠道和渠道组。先看看小米安全中心和爱奇艺是怎样划分渠道和渠道组的(小米6截图)

小米自带的安全中心的渠道划分

93e192b9a401?utm_campaign=maleskine

安全中心

可以看到,小米官方自带的安全中心已经做了通知渠道的划分,用户可以单独对某个渠道的通知进行设置,如声音、震动、锁屏通知、悬浮栏提示灯。

下面是爱奇艺的渠道划分

93e192b9a401?utm_campaign=maleskine

爱奇艺渠道划分

可以看到,爱奇艺的不仅划分了渠道,还给各个不同的渠道细分了渠道组。

下面首先介绍如何给自己的app添加通知渠道

首先创建一个test工程,targetSdkVersion设置为26

然后将下列代码放在 Activity 中。

onCreate 代码:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

String channelId = "chat";

String channelName = "聊天消息";

int importance = NotificationManager.IMPORTANCE_HIGH;

createNotificationChannel(channelId, channelName, importance);

channelId = "subscribe";

channelName = "订阅消息";

importance = NotificationManager.IMPORTANCE_DEFAULT;

createNotificationChannel(channelId, channelName, importance);

}

}

createNotificationChannel 代码:

@TargetApi(Build.VERSION_CODES.O)

private void createNotificationChannel(String channelId, String channelName, int importance) {

NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);

NotificationManager notificationManager = (NotificationManager) getSystemService(

NOTIFICATION_SERVICE);

if (notificationManager != null)

notificationManager.createNotificationChannel(channel);

}

运行程序后,打开app的通知管理就可以看到通知渠道了

它只会在首次运行的时候创建渠道,以后再运行此段代码,也不会重复创建。

93e192b9a401?utm_campaign=maleskine

爱奇艺渠道划分

但是这时候只是简单的划分了渠道,那么,怎么像爱奇艺一样,划分渠道组呢?

下面代码演示了如何划分渠道组:

onCreate:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

createNotifycationGroup("chat", "聊天");

createNotifycationGroup("subs", "普通消息");

String channelId = "chat";

String channelName = "聊天消息";

int importance = NotificationManager.IMPORTANCE_HIGH;

createNotificationChannel(channelId, "chat", channelName, importance);

channelId = "subscribe";

channelName = "订阅消息";

importance = NotificationManager.IMPORTANCE_DEFAULT;

createNotificationChannel(channelId, "subs", channelName, importance);

}

}

Method:

@TargetApi(Build.VERSION_CODES.O)

private void createNotificationChannel(String channelId, String group, String channelName, int importance) {

NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);

//在创建通知渠道前,指定渠道组 id

channel.setGroup(group);

NotificationManager notificationManager = (NotificationManager) getSystemService(

NOTIFICATION_SERVICE);

if (notificationManager != null)

notificationManager.createNotificationChannel(channel);

}

//创建一个渠道组

private void createNotifycationGroup(String groupId, String groupName) {

NotificationChannelGroup group = new NotificationChannelGroup(groupId, groupName);

NotificationManager notificationManager = (NotificationManager) getSystemService(

NOTIFICATION_SERVICE);

if (notificationManager != null) {

notificationManager.createNotificationChannelGroup(group);

}

}

通过上面代码,打开通知栏管理即可查看渠道组效果:

93e192b9a401?utm_campaign=maleskine

爱奇艺渠道划分

可以看到,消息划分成了 聊天 和 普通消息 渠道组,组下分别有不同的渠道

那么怎么往某个渠道发送消息呢?

下面代码演示如何发送通知栏消息:

public void sendMessage() {

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification notification = new NotificationCompat.Builder(this, "chat")

.setContentTitle("you girlfrind")

.setContentText("今晚上回家吗?")

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.drawable.icon)

.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))

.setAutoCancel(true)

.build();

manager.notify(1, notification);

}

把代码放在合适的位置,然后调用即可

93e192b9a401?utm_campaign=maleskine

爱奇艺渠道划分

重新运行程序后,即可在通知栏看到效果:

93e192b9a401?utm_campaign=maleskine

爱奇艺渠道划分

就这样了~~~

好了,这里只是介绍一下通知渠道和渠道组,更详细的可以看这篇博文,它介绍了如何适配app角标,设置消息的等级等内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值