长沙做php的网站建设,电商网站开发设计方法,专业的佛山网站建设公司,宁夏住房和城乡建设厅网站执业资格简介
通知是 Android 在您的应用 UI 之外显示的消息#xff0c;用于向用户提供提醒、来自其他人的通信或来自您的应用的其他及时信息。用户可以点击通知打开您的应用或直接从通知中执行操作。
2.1、展示
通知以不同的位置和格式向用户显示#xff0c;例如状态栏中的图标、…简介
通知是 Android 在您的应用 UI 之外显示的消息用于向用户提供提醒、来自其他人的通信或来自您的应用的其他及时信息。用户可以点击通知打开您的应用或直接从通知中执行操作。
2.1、展示
通知以不同的位置和格式向用户显示例如状态栏中的图标、通知抽屉中更详细的条目、应用程序图标上的徽章以及自动配对的可穿戴设备。当发出通知时它首先在状态栏中显示为一个图标。
2.2、操作
用户可以在状态栏上向下滑动以打开通知抽屉他们可以在其中查看更多详细信息并根据通知执行操作。用户可以向下拖动抽屉中的通知以显示展开的视图该视图显示其他内容和操作按钮如果提供。通知在通知抽屉中保持可见直到被应用程序或用户关闭。
3、功能拆解
本文将带领实现各种常见的通知功能以及各个Android版本需要做的适配。 4、功能实现
4.0、关键类
NotificationManager 通知管理器用来发起、更新、删除通知NotificationChannel 通知渠道8.0及以上配置渠道以及优先级NotificationCompat.Builder 通知构造器用来配置通知的布局显示以及操作相关 常用API查看第5节。 各版本适配查看第6节。 4.1、普通通知 private fun createNotificationForNormal() {// 适配8.0及以上 创建渠道if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mNormalChannelId, mNormalChannelName, NotificationManager.IMPORTANCE_LOW).apply {description 描述setShowBadge(false) // 是否在桌面显示角标}mManager.createNotificationChannel(channel)}// 点击意图 // setDeleteIntent 移除意图val intent Intent(this, MaterialButtonActivity::class.java)val pendingIntent PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)// 构建配置mBuilder NotificationCompat.Builder(thisNotificationActivity, mNormalChannelId).setContentTitle(普通通知) // 标题.setContentText(普通通知内容) // 文本.setSmallIcon(R.mipmap.ic_launcher) // 小图标.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_avatar)) // 大图标.setPriority(NotificationCompat.PRIORITY_DEFAULT) // 7.0 设置优先级.setContentIntent(pendingIntent) // 跳转配置.setAutoCancel(true) // 是否自动消失点击or mManager.cancel(mNormalNotificationId)、cancelAll、setTimeoutAfter()// 发起通知mManager.notify(mNormalNotificationId, mBuilder.build())}
复制代码
发起一个普通通知的几个要素
setContentTitle 标题setContentText 内容setSmallIcon 小图标setLargeIcon 大图标setPriority 优先级or重要性7.0和8.0的方式不同setContentIntent 点击意图setAutoCancel 是否自动取消notify 发起通知
4.2、重要通知 重要通知优先级设置最高会直接显示在屏幕内前台而不是只有通知抽屉里所以一定要谨慎设置不要引起用户的负面情绪。 private fun createNotificationForHigh() {val intent Intent(this, MaterialButtonActivity::class.java)val pendingIntent PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mHighChannelId, mHighChannelName, NotificationManager.IMPORTANCE_HIGH)channel.setShowBadge(true)mManager.createNotificationChannel(channel)}mBuilder NotificationCompat.Builder(thisNotificationActivity, mHighChannelId).setContentTitle(重要通知).setContentText(重要通知内容).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_avatar)).setAutoCancel(true).setNumber(999) // 自定义桌面通知数量.addAction(R.mipmap.ic_avatar, 去看看, pendingIntent)// 通知上的操作.setCategory(NotificationCompat.CATEGORY_MESSAGE) // 通知类别勿扰模式时系统会决定要不要显示你的通知.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) // 屏幕可见性锁屏时显示icon和标题内容隐藏mManager.notify(mHighNotificationId, mBuilder.build())}
复制代码
这里有几个新增的配置
setNumber 桌面通知数量addAction 通知上的操作setCategory 通知类别勿扰模式时系统会决定要不要显示你的通知setVisibility 屏幕可见性锁屏时显示icon和标题内容隐藏解锁查看全部
4.2.1、通知上的操作 可以通过addAction在通知上添加一个自定义操作如上图去看看。
可以通过PendingIntent打开一个Activity也可以是发送一个广播。
在Android10.0及以上系统也会默认识别并添加一些操作比如短信通知上的「复制验证码」。
4.2.2、重要性等级 紧急发出声音并显示为提醒通知高发出声音中没有声音低无声音且不出现在状态栏中
4.3、进度条通知 private fun createNotificationForProgress() {if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mProgressChannelId, mProgressChannelName, NotificationManager.IMPORTANCE_DEFAULT)mManager.createNotificationChannel(channel)}val progressMax 100val progressCurrent 30mBuilder NotificationCompat.Builder(thisNotificationActivity, mProgressChannelId).setContentTitle(进度通知).setContentText(下载中$progressCurrent%).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_avatar))// 第3个参数indeterminatefalse表示确定的进度比如100true表示不确定的进度会一直显示进度动画直到更新状态下载完成或删除通知.setProgress(progressMax, progressCurrent, false)mManager.notify(mProgressNotificationId, mBuilder.build())}
复制代码
比较常见的就是下载进度了比如应用内版本更新。
通过setProgress配置进度接收3个参数
max 最大值progress 当前进度indeterminate false表示确定的进度比如100true表示不确定的进度会一直显示进度动画直到更新状态完成或删除通知
如何更新进度往下看。
4.4、更新进度条通知 private fun updateNotificationForProgress() {if (::mBuilder.isInitialized) {val progressMax 100val progressCurrent 50// 1.更新进度mBuilder.setContentText(下载中$progressCurrent%).setProgress(progressMax, progressCurrent, false)// 2.下载完成//mBuilder.setContentText(下载完成).setProgress(0, 0, false)mManager.notify(mProgressNotificationId, mBuilder.build())Toast.makeText(this, 已更新进度到$progressCurrent%, Toast.LENGTH_SHORT).show()} else {Toast.makeText(this, 请先发一条进度条通知, Toast.LENGTH_SHORT).show()}}
复制代码
更新进度也还是通过setProgress修改当前进度值即可。
更新分为两种情况
更新进度修改进度值即可下载完成总进度与当前进度都设置为0即可同时更新文案
注意如果有多个进度通知如何更新到指定的通知是通过NotificationId匹配的。 Authoryechaoa 4.5、大文本通知 private fun createNotificationForBigText() {val bigText A notification is a message that Android displays outside your apps UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mBigTextChannelId, mBigTextChannelName, NotificationManager.IMPORTANCE_DEFAULT)mManager.createNotificationChannel(channel)}mBuilder NotificationCompat.Builder(thisNotificationActivity, mBigTextChannelId).setContentTitle(大文本通知).setStyle(NotificationCompat.BigTextStyle().bigText(bigText)).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_avatar)).setAutoCancel(true)mManager.notify(mBigTextNotificationId, mBuilder.build())}
复制代码
setStyle(NotificationCompat.BigTextStyle().bigText(bigText))
通知内容默认最多显示一行超出会被裁剪且无法展开在内容透出上体验非常不好展示的内容可能无法吸引用户去点击查看所以也有了大文本通知的这种方式
一劳永逸的做法就是无论内容有多少行都用大文本的这种方式通知具体展示让系统自己去适配。
4.6、大图片通知 private fun createNotificationForBigImage() {val bigPic BitmapFactory.decodeResource(resources, R.drawable.ic_big_pic)if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mBigImageChannelId, mBigImageChannelName, NotificationManager.IMPORTANCE_DEFAULT)mManager.createNotificationChannel(channel)}mBuilder NotificationCompat.Builder(thisNotificationActivity, mBigImageChannelId).setContentTitle(大图片通知).setContentText(有美女展开看看).setStyle(NotificationCompat.BigPictureStyle().bigPicture(bigPic)).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_avatar)).setAutoCancel(true)mManager.notify(mBigImageNotificationId, mBuilder.build())}
复制代码
与大文本通知方式差不多
setStyle(NotificationCompat.BigPictureStyle().bigPicture(bigPic))
有一个注意的点当已有多条通知时默认是合并的并不是展开的所以可以通过setContentText(有美女展开看看)加个提示。
当前应用的通知不超过3条会展开超过3条通知会聚合并折叠
4.7、自定义通知 private fun createNotificationForCustom() {// 适配8.0及以上if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mCustomChannelId, mCustomChannelName, NotificationManager.IMPORTANCE_DEFAULT)mManager.createNotificationChannel(channel)}// 适配12.0及以上mFlag if (Build.VERSION.SDK_INT Build.VERSION_CODES.S) {PendingIntent.FLAG_IMMUTABLE} else {PendingIntent.FLAG_UPDATE_CURRENT}// 添加自定义通知viewval views RemoteViews(packageName, R.layout.layout_notification)// 添加暂停继续事件val intentStop Intent(mStopAction)val pendingIntentStop PendingIntent.getBroadcast(thisNotificationActivity, 0, intentStop, mFlag)views.setOnClickPendingIntent(R.id.btn_stop, pendingIntentStop)// 添加完成事件val intentDone Intent(mDoneAction)val pendingIntentDone PendingIntent.getBroadcast(thisNotificationActivity, 0, intentDone, mFlag)views.setOnClickPendingIntent(R.id.btn_done, pendingIntentDone)// 创建BuildermBuilder NotificationCompat.Builder(thisNotificationActivity, mCustomChannelId).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_avatar)).setAutoCancel(true).setCustomContentView(views).setCustomBigContentView(views)// 设置自定义通知view// 发起通知mManager.notify(mCustomNotificationId, mBuilder.build())}
复制代码
假如是一个播放器的前台通知默认的布局显示已经不满足需求那么就用到自定义布局了。
通过RemoteViews构建自定义布局view。因为RemoteViews并不是一个真正的view它只是一个view的描述所以事件处理上还是要借助PendingIntent。
setCustomContentView 默认布局显示即折叠状态下的布局setCustomBigContentView 展开状态下的布局
折叠状态下可能会展示一些基础信息拿播放器举例比如当前歌曲名称、歌手、暂停、继续、下一首等就差不多展示不下了。 展开状态下就可以提供更多的信息比如专辑信息歌手信息等
这两种状态下默认的布局高度
折叠视图布局48dp展开视图布局252dp
4.8、更新自定义通知 private fun updateNotificationForCustom() {// 发送通知 更新状态及UIsendBroadcast(Intent(mStopAction))}private fun updateCustomView() {val views RemoteViews(packageName, R.layout.layout_notification)val intentUpdate Intent(mStopAction)val pendingIntentUpdate PendingIntent.getBroadcast(this, 0, intentUpdate, mFlag)views.setOnClickPendingIntent(R.id.btn_stop, pendingIntentUpdate)// 根据状态更新UIif (mIsStop) {views.setTextViewText(R.id.tv_status, 那些你很冒险的梦-停止播放)views.setTextViewText(R.id.btn_stop, 继续)mBinding.mbUpdateCustom.text 继续} else {views.setTextViewText(R.id.tv_status, 那些你很冒险的梦-正在播放)views.setTextViewText(R.id.btn_stop, 暂停)mBinding.mbUpdateCustom.text 暂停}mBuilder.setCustomContentView(views).setCustomBigContentView(views)// 重新发起通知更新UI注意必须得是同一个通知id即mCustomNotificationIdmManager.notify(mCustomNotificationId, mBuilder.build())}
复制代码
上面提到因为RemoteViews并不能直接操作view所以可以通过广播的方式对该条通知的构建配置重新设置以达到更新的效果。 远古时期v4包里还有MediaStyleAndroidX已经下掉了。 5、常用API
API描述setContentTitle标题setContentText内容setSubText子标题setLargeIcon大图标setSmallIcon小图标setContentIntent点击时意图setDeleteIntent删除时意图setFullScreenIntent全屏通知点击意图来电、响铃setAutoCancel点击自动取消setCategory通知类别适用“勿扰模式”setVisibility屏幕可见性适用“锁屏状态”setNumber通知项数量setWhen通知时间setShowWhen是否显示通知时间setSound提示音setVibrate震动setLights呼吸灯setPriority优先级7.0setTimeoutAfter定时取消8.0及以后setProgress进度setStyle通知样式BigPictureStyle、BigTextStyle、MessagingStyle、InboxStyle、DecoratedCustomViewStyleaddAction通知上的操作10.0setGroup分组setColor背景颜色
6、各版本适配
自Android 4.0支持通知以来几乎每个版本都有各种改动也是苦了开发了...
6.1、Android 5.0
6.1.1、重要通知
Android 5.0开始支持重要通知也称抬头通知。
6.1.2、锁屏通知
Android 5.0开始支持锁屏通知即锁屏时显示在锁屏桌面。
从8.0开始用户可以通过通知渠道设置启用或禁止锁屏通知...
6.1.3、勿扰模式
5.0开始勿扰模式下会组织所有声音和震动8.0以后可以根据渠道分别设置。
6.2、Android 7.0
6.2.1、设置通知优先级
7.1及以下 mBuilder NotificationCompat.Builder(thisNotificationActivity, mNormalChannelId)....setPriority(NotificationCompat.PRIORITY_DEFAULT) // 7.0 设置优先级
复制代码
8.0及以上改为渠道。
6.2.2、回复操作
7.0引入直接回复操作的功能
private val KEY_TEXT_REPLY key_text_reply
var replyLabel: String resources.getString(R.string.reply_label)
var remoteInput: RemoteInput RemoteInput.Builder(KEY_TEXT_REPLY).run {setLabel(replyLabel)build()
}
复制代码
6.2.3、消息通知
7.0开始支持消息类型通知MessagingStyle
var notification NotificationCompat.Builder(this, CHANNEL_ID).setStyle(NotificationCompat.MessagingStyle(Me).setConversationTitle(Team lunch).addMessage(Hi, timestamp1, null) // Pass in null for user..addMessage(Whats up?, timestamp2, Coworker).addMessage(Not much, timestamp3, null).addMessage(How about lunch?, timestamp4, Coworker)).build()
复制代码
从8.0开始消息类型的展示方式为折叠类型...
6.2.4、通知分组
7.0开始通知支持分组适用多个通知的情况。
6.3、Android 8.0
6.3.1、创建通知渠道
创建通知渠道以及重要性 if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {val channel NotificationChannel(mHighChannelId, mHighChannelName, NotificationManager.IMPORTANCE_HIGH)mManager.createNotificationChannel(channel)}
复制代码
删除渠道
notificationManager.deleteNotificationChannel(id)
复制代码
6.3.2、通知角标
8.0开始支持设置通知时桌面的角标是否显示
val mChannel NotificationChannel(id, name, importance).apply {description descriptionTextsetShowBadge(false)
}
复制代码
6.3.3、通知限制
8.1开始每秒发出的通知声音不能超过一次。
6.3.4、背景颜色
8.0开始设置通知的背景颜色。
6.4、Android 10.0
6.4.1、添加操作 mBuilder NotificationCompat.Builder(thisNotificationActivity, mHighChannelId)....addAction(R.mipmap.ic_avatar, 去看看, pendingIntent)// 通知上的操作
复制代码
6.4.2、全屏意图
10.0全屏意图需要在manifest中申请USE_FULL_SCREEN_INTENT权限
6.5、Android 12.0
6.5.1、解锁设备
12.0及以上可以设置需要解锁设备才能操作setAuthenticationRequired
val moreSecureNotification Notification.Builder(context, NotificationListenerVerifierActivity.TAG).addAction(...)// from a lock screen..setAuthenticationRequired(true).build()
复制代码
6.5.2、自定义通知
从12.0开始将不支持完全自定义的通知会提供 Notification.DecoratedCustomViewStyle替代...
6.5.3、PendingIntent
12.0需要明确设置flag否则会有报错 java.lang.IllegalArgumentException: com.example.imdemo: Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. mFlag if (Build.VERSION.SDK_INT Build.VERSION_CODES.S) {PendingIntent.FLAG_IMMUTABLE} else {PendingIntent.FLAG_UPDATE_CURRENT}val intentStop Intent(mStopAction)val pendingIntentStop PendingIntent.getBroadcast(thisNotificationActivity, 0, intentStop, mFlag)views.setOnClickPendingIntent(R.id.btn_stop, pendingIntentStop)
复制代码 梳理完适配真的觉得Androider苦 7、Github
github.com/yechaoa/Mat… 作者yechaoa 链接https://juejin.cn/post/7113509911887085581 来源稀土掘金 著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。