Android 通知渠道Notification Channel

Android8.0也就是API26开始要求通知设置Channel,否则会报错

 

查看官方通知:

通知

在 Android 8.0 中,我们已重新设计通知,以便为管理通知行为和设置提供更轻松和更统一的方式。这些变更包括:

Android 8.0 中的通知长按菜单。

通知渠道:Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将
通知渠道称之为通知类别。要了解如何实现通知渠道的信息,请参阅通知渠道指南。

通知标志:Android 8.0 引入了对在应用启动器图标上显示通知标志的支持。通知标志可反映某个应用是否存在与其
关联、并且用户尚未予以清除也未对其采取行动的通知。通知标志也称为通知点。要了解如何调整通知标志,请参阅通知标志指南。

休眠:用户可以将通知置于休眠状态,以便稍后重新显示它。重新显示时通知的重要程度与首次显示时相同。应用可以移除或更新已休眠的通知,但更新休眠的通知并不会使其重新显示。

通知超时:现在,使用 setTimeoutAfter() 创建通知时您可以设置超时。您可以使用此函数指定一个持续时间,超过该持续时间后,通知应取消。如果需要,您可以在指定的超时持续时间之前取消通知。

通知设置:当您使用 Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCESIntent 从通知创建指向应用通知设置的链接时,您可以调用 setSettingsText() 来设置要显示的文本。此系统可以提供以下 Extra 数据和 Intent,用于过滤应用必须向用户显示的设置:EXTRA_CHANNEL_ID、NOTIFICATION_TAG 和 NOTIFICATION_ID。

通知清除:系统现在可区分通知是由用户清除,还是由应用移除。要查看清除通知的方式,您应实现 NotificationListenerService 类的新 onNotificationRemoved() 函数。

背景颜色:您现在可以设置和启用通知的背景颜色。只能在用户必须一眼就能看到的持续任务的通知中使用此功能。例如,您可以为与驾车路线或正在进行的通话有关的通知设置背景颜色。您还可以使用 Notification.Builder.setColor() 设置所需的背景颜色。这样做将允许您使用 Notification.Builder.setColorized() 启用通知的背景颜色设置。

消息样式:现在,使用 MessagingStyle 类的通知可在其折叠形式中显示更多内容。对于与消息有关的通知,您应使用 MessagingStyle 类。您还可以使用新的 addHistoricMessage() 函数,通过向与消息相关的通知添加历史消息为会话提供上下文。

 

Android官方还在GitHub上提交了示例代码:

https://github.com/googlesamples/android-NotificationChannels/ 

官方API文档

https://developer.android.com/reference/android/app/NotificationChannel

 

设置好通知Channel还可以在手机设置→通知里面看到每个APP的自己的通知Channel

 

构造方法

三个参数分别为ID,名字,重要度

NotificationChannel(String id,CharSequence name, int importance)

几个重要度:

 * IMPORTANCE_NONE 关闭通知
 * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
 * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
 * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
 * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示

 

示例代码

        NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = null;
        //Android8.0要求设置通知渠道
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel("foreground", "foregroundName", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

在使用通知或者前台服务的时候要设置Channel的id

notification.setChannelId("foreground")

channel可以设置震动,通知灯闪烁等请自行去看官方API

 

我们还可以创建渠道组,然后绑定Channel(不知道有啥用,研究研究)

// 创建一个渠道组
NotificationChannelGroup channelGroup = new NotificationChannelGroup("测试组ID", "渠道组名");
// 绑定渠道组
channel.setGroup("测试组ID");
notificationManager.createNotificationChannelGroup(channelGroup);

 

查询删除渠道和渠道组

             // 查询所有当前用户的所有渠道
            notificationManager.getNotificationChannels();
            // 查询所有当前用户的所有渠道组
            notificationManager.getNotificationChannelGroups();
            // 根据ID删除渠道
            notificationManager.deleteNotificationChannel("渠道ID");
            // 根据ID删除渠道组
            notificationManager.deleteNotificationChannel("测试组ID");

 

 

  • 12
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android中创建通知,需要遵循以下步骤: 1. 创建通知渠道(可选) 如果您使用的是Android 8.0及更高版本,则需要创建通知渠道通知渠道是一种分类通知的方式,可以帮助用户更好地管理他们收到的通知。可以使用以下代码创建通知渠道: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channelId", "channelName", NotificationManager.IMPORTANCE_DEFAULT); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } ``` 2. 创建通知 使用以下代码创建通知: ``` NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelId") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 在这个示例中,我们使用了通知渠道通知的ID为notificationId,通知的内容包括一个小图标、标题和正文。 3. 自定义通知 您可以根据需要自定义通知,例如添加操作按钮或更改通知的外观。以下是一些自定义通知的示例: 添加操作按钮: ``` Intent intent = new Intent(this, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelId") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .addAction(R.drawable.ic_action_name, "Action", pendingIntent); ``` 自定义通知的外观: ``` NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelId") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setStyle(new NotificationCompat.BigTextStyle() .bigText("Much longer text that cannot fit one line...")) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon)); ``` 4. 显示通知 使用以下代码显示通知: ``` NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 在这个示例中,我们使用了通知渠道通知的ID为notificationId,通知的内容包括一个小图标、标题和正文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值