- Date:
MM/DD/YYYY
- Auther: HABIN
- 设备: AS模拟器7.1.1 AS模拟器9.0
ℹ️NOTE
写通知渠道用的原因是自己在改官方系统时钟7.1.1(从aosp7.1.1上拉下来的)然后运行到9.0 遇到所有时钟通知都会主动弹出(需求有些不需要主动弹出),一开始以为是通知优先级内容setPriority设置问题(8.0以下确实是这个方法控制),后面发现是渠道的原因,记录这篇是为了方便以后翻阅 本文内容基于官方内容整理https://developer.android.google.cn/guide/topics/ui/notifiers/notifications#importance
:
通知渠道
从 Android 8.0(API 级别 26)开始, 所有通知都必须分配到相应的渠道,否则通知将不会显示。对于每个渠道,可以单独设置是否显示,是否响铃,设置响铃等。然后,用户可以更改这些设置,并确定应用中的哪些通知渠道应具有干扰性或应该可见。
文章应用NotificationChannelDemo是一个demo,用kotlin语言写的 链接在文章最后 有兴趣可以从github拉下来看看
Android8.0与7.1以下区别
1)Android7.1.1以下
界面入口的话基本都是一样 找到当前app应用信息界面,找到通知列表,可以看到所有通知都是统一管理,设置阻止 所有组织 设置不发出声音所有都不能发出声音

Android7 - 创建通知的方式
//这段代码Demo中没有的
var mNotificationManager:NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_small_notify)//通知图标
.setContentTitle(title)//标题
.setContentText(content)//通知内容
//.setPriority(NotificationCompat.PRIORITY_DEFAULT)//如果不舍 默认是PRIORITY_DEFAULT 这里就是与Android8.0+渠道不同的地方
//真正发起通知 这里两个参数 第一个参数就是标识这个通知
//如果要手动代码关闭当前通知 就需要这个notificationId来取消 调用方法 mNotificationManager.cancel(0/*id*/)
// 第二个参数就是通知内容
mNotificationManager.notify(0,notification.build())
2)Android8.0+(下面设备是9.0的模拟器)
9.0也是一样 找到当前
Android8+ 创建通知的方式
这里是demo中主要内容的代码 渠道类别有几种 这里我只展示了常用的两种 这里只是命名而已 可以随意写id 与name的内容 只是为了标识
NotificationChannel(String id, CharSequence name, int importance) 类就是创建渠道的,id就是渠道id,每新增一条通知就需要配置相应的渠道id,name用户应用通知详情渠道列表的名字,importance参数就是本文的重点 渠道重要性 控制着通知是否能够显示 能够弹窗 有提示音。
/**
* 通知渠道 大部分厂家通知渠道归类通知类别
* channelId 这个是一个标识 Android8.0+每个通知都要加这个channelId标识重要程度 是否弹窗 是否通知 是否发出声音
* channelName 这个可以自己随意命名定义 安装后 到当前应用详情通知功能看到这个名字 创建多少个渠道就有多少条,
* 用于手机查看每个通知渠道 这是用于用户可以单独设置每个通知渠道的显示程度
* TIP: 同一种重要程度渠道可以创建多个,channelID 和channelName设置不同就行 这里我只写了官方重要级别到几种
* */
//IMPORTANCE_HIGH 紧急:发出提示音,并以浮动通知的形式显示
const val HIGH_CHANNEL_ID: String = "highChannelId"
const val HIGH_CHANNEL_NAME: String = "紧急通知"
//IMPORTANCE_LOW 中: 显示通知但不发出提示音
const val LOW_CHANNEL_ID: String = "lowChannelId"
const val LOW_CHANNEL_NAME: String = "中通知"
var mNotificationManager:NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val highChannel = NotificationChannel(
HIGH_CHANNEL_ID,
HIGH_CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH//真正设置通知重要性级别就是这个参数
)
//需要几个通知就创建多少个NotificationChannel
mNotificationManager.createNotificationChannel(highChannel)
//中通知
val lowChannel = NotificationChannel(
LOW_CHANNEL_ID,
LOW_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
)
mNotificationManager.createNotificationChannel(lowChannel)
var highNotification = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("紧急通知标题")
.setContentText("紧急通知内容")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
var lowNotification = NotificationCompat.Builder(this, LOW_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("中通知标题")
.setContentText("中通知内容")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
//发起两个通知
mNotificationManager.notify(0,highNotification.build())
mNotificationManager.notify(1,lowNotification.build())
当createNotificationChannel()初始化后 在应用通知列表中就会显示两个渠道
下面是demo中点击后弹窗级别显示
表 1. 渠道重要性级别
用户可见的重要性级别 | 重要性(Android 8.0 及更高版本) | 优先级(Android 7.1 及更低版本) |
---|---|---|
紧急 :发出提示音,并以浮动通知的形式显示 | IMPORTANCE_HIGH | PRIORITY_HIGH 或 PRIORITY_MAX |
高 :发出提示音 | IMPORTANCE_DEFAULT | PRIORITY_DEFAULT |
中 :无提示音 | IMPORTANCE_LOW | PRIORITY_LOW |
低 :无提示音,且不会在状态栏中显示。 | IMPORTANCE_MIN | PRIORITY_MIN |
demo中重点代码
/***
* author : habin
* time : 2022/04/27
* desc :
*/
class NotificationBuilder {
val TAG: String = "NotificationBuilder"
fun builderNotify(
context: Context,
channelId: String,
title: String = "通知标题",
content: String = "通知内容!!!!!",
pri: Int = NotificationCompat.PRIORITY_DEFAULT
): Notification {
Log.d(TAG, "builderHighNotify: 添加通知")
val notification = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_small_notify)
.setContentTitle(title)
.setContentText(content)
.setPriority(pri)
return notification.build()
}
fun builderHighNotify(
context: Context,
title: String = "紧急通知标题",
content: String = "紧急通知内容!!!!!",
pri: Int = NotificationCompat.PRIORITY_DEFAULT
): Notification {
Log.d(TAG, "builderHighNotify: 添加紧急通知")
val notification = NotificationCompat.Builder(context, HIGH_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_small_notify)
.setContentTitle(title)
.setContentText(content)
.setPriority(pri)
return notification.build()
}
fun builderDefaultNotify(
context: Context,
title: String = "高通知标题",
content: String = "高通知内容!!!!!",
pri: Int = NotificationCompat.PRIORITY_DEFAULT
): Notification {
Log.d(TAG, "builderHighNotify: 添加高通知")
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_small_notify)
.setContentTitle(title)
.setContentText(content)
.setPriority(pri)
return notification.build()
}
fun builderLowNotify(
context: Context,
title: String = "中通知标题",
content: String = "中通知内容!!!!!",
pri: Int = NotificationCompat.PRIORITY_DEFAULT
): Notification {
Log.d(TAG, "builderHighNotify: 添加中通知")
val notification = NotificationCompat.Builder(context, LOW_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_small_notify)
.setContentTitle(title)
.setContentText(content)
.setPriority(pri)
return notification.build()
}
fun builderMinNotify(
context: Context,
title: String = "低通知标题",
content: String = "低通知内容!!!!!",
pri: Int = NotificationCompat.PRIORITY_DEFAULT
): Notification {
val notification = NotificationCompat.Builder(context, MIN_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_small_notify)
.setContentTitle(title)
.setContentText(content)
.setPriority(pri)
return notification.build()
}
}
/**
* 通知渠道 大部分厂家通知渠道归类通知类别
* channelId 这个是一个标识 Android8.0+每个通知都要加这个channelId标识重要程度 是否弹窗 是否通知 是否发出声音
* channelName 这个可以自己随意命名定义 安装后 到当前应用详情通知功能看到这个名字 创建多少个渠道就有多少条,
* 用于手机查看每个通知渠道 这是用于用户可以单独设置每个通知渠道的显示程度
* TIP: 同一种重要程度渠道可以创建多个,channelID 和channelName设置不同就行 这里我只写了官方重要级别到几种
* */
//IMPORTANCE_HIGH 紧急:发出提示音,并以浮动通知的形式显示
const val HIGH_CHANNEL_ID: String = "highChannelId"
const val HIGH_CHANNEL_NAME: String = "紧急通知"
//IMPORTANCE_DEFAULT 高:发出提示音
const val DEFAULT_CHANNEL_ID: String = "defaultChannelId"
const val DEFAULT_CHANNEL_NAME: String = "高通知"
//IMPORTANCE_LOW 中: 显示通知但不发出提示音
const val LOW_CHANNEL_ID: String = "lowChannelId"
const val LOW_CHANNEL_NAME: String = "中通知"
//IMPORTANCE_MIN 低: 无提示音,且不会在状态栏中显示
const val MIN_CHANNEL_ID: String = "minChannelId"
const val MIN_CHANNEL_NAME: String = "低通知"
/**
* TODO:
* 增加这个紧急2通知 是想说明下渠道id name不是唯一的 可以创建多个紧急渠道
* 像企业微信 多个紧急通知 比如新消息通知 新邮件提醒等 高通知也有多个这样
* 需要多少个就创建多少个通知渠道(通知类别)这里我就不创建了
* val high2Channel = NotificationChannel(HIGH2_CHANNEL_ID,HIGH2_CHANNEL_NAME,
* NotificationManager.IMPORTANCE_HIGH)
* mNotificationManager.createNotificationChannel(high2Channel)
*/
const val HIGH2_CHANNEL_ID: String = "high2ChannelId"
const val HIGH2_CHANNEL_NAME: String = "紧急2通知"
/***
* author : habin
* time : 2022/04/27
* desc : 通知渠道管理模块
*/
class NotifyManager(context: Context) {
var mNotificationManager:NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
init {
val TAG = "NotifyManager"
Log.e(TAG, "初始化通知渠道: " )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//紧急通知
val highChannel = NotificationChannel(
HIGH_CHANNEL_ID,
HIGH_CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH
)
mNotificationManager.createNotificationChannel(highChannel)
//高通知通知
val defaultChannel = NotificationChannel(
DEFAULT_CHANNEL_ID,
DEFAULT_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
mNotificationManager.createNotificationChannel(defaultChannel)
//中通知
val lowChannel = NotificationChannel(
LOW_CHANNEL_ID,
LOW_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
)
mNotificationManager.createNotificationChannel(lowChannel)
//低通知
val minChannel = NotificationChannel(
MIN_CHANNEL_ID,
MIN_CHANNEL_NAME,
NotificationManager.IMPORTANCE_MIN
)
mNotificationManager.createNotificationChannel(minChannel)
}
}
companion object {
@Volatile
var instance: NotifyManager? = null
fun getInstance(context: Context): NotifyManager {
if (instance == null) {
synchronized(NotifyManager::class) {
if (instance == null) {
instance = NotifyManager(context)
}
}
}
return instance!!
}
}
fun addNotify(notification: Notification,notifyId:Int = 0) {
mNotificationManager.notify(notifyId,notification)
}
/**
* 取消所有通知
* */
fun cancelAllNotify(): Unit {
with(mNotificationManager) {
cancelAll()
}
}
}