当前位置: 首页 > news >正文

AR 眼镜之-系统通知定制(通知弹窗)-实现方案

目录

📂 前言

AR 眼镜系统版本

系统通知定制

1. 🔱 技术方案

1.1 技术方案概述

1.2 实现方案

1)实现系统通知的监听

2)系统通知显示:通知弹窗

2. 💠 实现系统通知的监听

2.1 继承 NotificationListenerService

2.2 在 manifest 中声明这个可接收通知的服务

2.3 让通知应用拥有获取系统通知的权限

1)通知应用申明可获取系统通知使用权限

2)判断通知应用是否拥有可获取系统通知的权限

3)打开通知权限设置页面

3. ⚛️ 系统通知显示:通知弹窗

3.1 统一处理通知

1)每条通知到来时由 handleNotification 分发路由

2)NotificationManagerBean 区分 AR 眼镜通知以及与 AR 眼镜连接的手机通知

3)飞行模式时不显示系统通知

3.2 播放通知音效

3.3 showDialog 显示通知弹窗

1)NotificationLayoutDialogBinding 加载通知弹窗 View

2)getAppName 获取 app 名

3)showNotification 显示通知弹窗 View

4. ✅ 小结

附录1:SystemUI 流程

附录2:使用 NotificationListenerService 监听通知


📂 前言

AR 眼镜系统版本

        W517 Android9。

系统通知定制

        系统通知的底层 实现主要依赖 Android 原生通知模块 NotificationManagerService系统通知的上层 UI 主要依赖于继承 NotificationListenerService 去实现,实现过程如下图所示,主要分为三步:1)应用 A 通过 sendNotification 发送通知;2)Android 通知模块 NotificationManagerService 接收到通知;3、应用 B 通过继承 NotificationListenerService 监听到系统通知。对于底层实现感兴趣的同学可自行去深入了解,本文所讨论的系统通知实现方案主要针对于上层 UI。

        那么,Android 原生系统通知是怎样实现的呢?答案很简单:通过 SystemUI 应用实现,SystemUI 通过继承 NotificationListenerService 监听系统通知,然后显示在通知栏。

        但是,AR 眼镜系统与传统 Android 2D 存在较大显示与交互差异,且根据产品需求综合来看,本文采用类似 SystemUI 的方案,通知应用 通过继承 NotificationListenerService 实现系统通知的监听与显示。

1. 🔱 技术方案

1.1 技术方案概述

        通知应用 通过继承 NotificationListenerService 实现系统通知的监听与显示,上层 UI 主要包括:通知弹窗、通知中心,系统通知定制的实现方案将分为两个篇章展开,分别是 通知弹窗篇通知中心篇

1.2 实现方案

1)实现系统通知的监听
  1. 继承 NotificationListenerService,实现 onNotificationPosted 方法;

  2. 在 manifest 中声明这个可接收通知的服务;

  3. 让通知应用拥有获取系统通知的权限。

2)系统通知显示:通知弹窗
  1. 统一处理通知;

  2. 播放通知音效;

  3. 显示与隐藏通知弹窗。

2. 💠 实现系统通知的监听

2.1 继承 NotificationListenerService

        主要实现其中的 onNotificationPosted(sbn: StatusBarNotification) 方法,其他方法可按需实现,如: onNotificationRemoved(sbn: StatusBarNotification)、onListenerConnected()、onListenerDisconnected()。

class AGGNotificationListenerService : NotificationListenerService() {override fun onNotificationPosted(sbn: StatusBarNotification) {super.onNotificationPosted(sbn)Log.i(TAG, "onNotificationPosted: packageName = ${sbn.packageName}")// 普通通知:未设置Style// 设置点击 setContentIntent// 设置按钮 addAction(最多可添加三个)// 设置进度条 setProgress// 设置自定义通知 setCustomContentView(RemoteViews)// 设置自定义通知展开视图 setCustomBigContentView(RemoteViews)// 设置自定义顶部提醒视图 setCustomHeadsUpContentView(RemoteViews(context.getPackageName(),R.layout.custom_heads_up_layout))// 带图标样式 setLargeIcon// 1. 过滤黑名单包名的通知。if (BLACK_LISTING_PACKAGE_NAME.contains(sbn.packageName)) return// 2. 过滤空内容消息通知val title = sbn.notification.extras.getString(Notification.EXTRA_TITLE, "")val content = sbn.notification.extras.getCharSequence(Notification.EXTRA_TEXT, "")if (title.isEmpty() && content.isEmpty()) returnif (content == getString(R.string.app_running_notification_text)) return // 去掉通知: “记录”正在运行,点按即可了解详情或停止应用AGGNotificationManager.handleNotification(this, NotificationManagerBean(NotificationManagerBean.FROM_GLASS, sbn))}override fun onNotificationRemoved(sbn: StatusBarNotification) {super.onNotificationRemoved(sbn)Log.i(TAG, "onNotificationRemoved: packageName = ${sbn.packageName}")}override fun onListenerConnected() {super.onListenerConnected()Log.i(TAG, "onListenerConnected: ")}override fun onListenerDisconnected() {super.onListenerDisconnected()Log.i(TAG, "onListenerDisconnected: ")}companion object {private val TAG = AGGNotificationListenerService::class.java.simpleNameprivate val BLACK_LISTING_PACKAGE_NAME =// Android系统通知、Android电话通知mutableSetOf("android", "com.android.dialer", "com.android.server.telecom")}}

2.2 在 manifest 中声明这个可接收通知的服务

<serviceandroid:name=".AGGNotificationListenerService"android:exported="true"android:label="AGG Notification"android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"><intent-filter><action android:name="android.service.notification.NotificationListenerService" /></intent-filter>
</service>

2.3 让通知应用拥有获取系统通知的权限

1)通知应用申明可获取系统通知使用权限
<uses-permission android:name="android.permission.MANAGE_NOTIFICATIONS" />
2)判断通知应用是否拥有可获取系统通知的权限
fun isNotificationListenersEnabled(context: Context, packageName: String): Boolean = NotificationManagerCompat.getEnabledListenerPackages(context).contains(packageName)
3)打开通知权限设置页面
fun gotoNotificationAccessSetting(context: Context): Boolean {return try {val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)context.startActivity(intent)true} catch (e: ActivityNotFoundException) {// 普通情况下找不到的时候需要再特殊处理找一次try {val intent = Intent()intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)val cn = ComponentName("com.android.settings","com.android.settings.Settings\$NotificationAccessSettingsActivity")intent.component = cnintent.putExtra(":settings:show_fragment", "NotificationAccessSettings")context.startActivity(intent)return true} catch (e1: java.lang.Exception) {e1.printStackTrace()}Toast.makeText(this, "对不起,您的手机暂不支持", Toast.LENGTH_SHORT).show()e.printStackTrace()false}
}

注:如若获取不到系统通知,可参考本文末尾的附录2:使用 NotificationListenerService 监听通知。

3. ⚛️ 系统通知显示:通知弹窗

3.1 统一处理通知

1)每条通知到来时由 handleNotification 分发路由
object AGGNotificationManager {private val TAG = AGGNotificationManager::class.java.simpleName/*** 处理通知,每条通知到来时先经过此处路由。*/fun handleNotification(context: Context, notificationManagerBean: NotificationManagerBean) {// ...}}
2)NotificationManagerBean 区分 AR 眼镜通知以及与 AR 眼镜连接的手机通知
data class NotificationManagerBean(@FromType var from: Int = FROM_NONE, // 通知来源:1:眼镜;2:手机var glassNotification: StatusBarNotification? = null, //眼镜通知var phoneNotification: MessageReqMsgNoti? = null, // 手机通知
) {@IntDef(FROM_NONE, FROM_GLASS, FROM_PHONE)@Retention(AnnotationRetention.SOURCE)annotation class FromTypecompanion object {const val FROM_NONE = -1const val FROM_GLASS = 1const val FROM_PHONE = 2}
}
3)飞行模式时不显示系统通知
object AGGNotificationManager {private val TAG = AGGNotificationManager::class.java.simpleName/*** 处理通知,每条通知到来时先经过此处路由。*/fun handleNotification(context: Context, notificationManagerBean: NotificationManagerBean) {if (isAirPlaneMode(context)) {Log.i(TAG, "handleNotification: isAirPlaneMode = true.")return}// ...}/*** 是否在飞行模式*/fun isAirPlaneMode(context: Context): Boolean = Settings.Global.getInt(context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) == 1    }

3.2 播放通知音效

SoundPoolTools.playNotifyCome(context.applicationContext)

        参考系统应用音效播放即可:AR 眼镜之-系统应用音效-实现方案-CSDN博客

3.3 showDialog 显示通知弹窗

1)NotificationLayoutDialogBinding 加载通知弹窗 View
showDialog(context: Context,packageName: String,smallIcon: Drawable?,title: String,content: CharSequence
){val binding = NotificationLayoutDialogBinding.inflate(LayoutInflater.from(context)).apply {itemInfoLeftIcon.setImageDrawable(smallIcon)itemInfoMsg.text = getAppName(context, packageName)itemTitle.text = titleitemContent.text = content}AGGSuspensionNotification.showNotification(context, binding.root)
}
2)getAppName 获取 app 名
fun getAppName(context: Context, packageName: String): String {return try {val pm = context.packageManagerval pi = pm.getPackageInfo(packageName, 0)pi?.applicationInfo?.loadLabel(pm)?.toString() ?: packageName} catch (e: Exception) {packageName}
}
3)showNotification 显示通知弹窗 View
object AGGSuspensionNotification {private val TAG = AGGSuspensionNotification::class.java.simpleNameprivate var mWindowManager: WindowManager? = nullprivate var mLayoutParams: WindowManager.LayoutParams? = nullprivate var mCustomView: View? = nullfun showNotification(context: Context, customView: View) {mCustomView = customViewinitLayoutParams(context)if (!customView.isAttachedToWindow) {kotlin.runCatching {Log.i(TAG, "showNotification: addView")mWindowManager?.addView(customView, mLayoutParams)}}}fun removeNotification() {Log.i(TAG, "removeNotification: ")if (mCustomView?.isAttachedToWindow == true) {kotlin.runCatching {Log.i(TAG, "removeNotification: removeViewImmediate")mWindowManager?.removeViewImmediate(mCustomView)}mCustomView = null}}private fun initLayoutParams(context: Context) {Log.i(TAG, "initLayoutParams: ")mWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManagermLayoutParams = WindowManager.LayoutParams().apply {type = WindowManager.LayoutParams.TYPE_MAGNIFICATION_OVERLAYflags =(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH or WindowManager.LayoutParams.FLAG_SPLIT_TOUCH or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)format = PixelFormat.TRANSLUCENTwidth = (840 * context.resources.displayMetrics.density + 0.5f).toInt()height = (840 * context.resources.displayMetrics.density + 0.5f).toInt()gravity = Gravity.CENTER_HORIZONTAL or Gravity.TOPx = -100y = -100title = TAG + "_MASK"// dofIndex = 0// setTranslationZ(TRANSLATION_Z_150CM)}}}

注:对于通知弹窗的消失,以及通知中心显示与交互,由于篇幅问题,将放在下一篇章。AR 眼镜之-系统通知定制(通知中心)-实现方案-CSDN博客

4. ✅ 小结

        对于系统通知定制(通知弹窗),本文只是一个基础实现方案,更多业务细节请参考产品逻辑去实现。

        另外,由于本人能力有限,如有错误,敬请批评指正,谢谢。


附录1:SystemUI 流程

SystemUI流程_systemui启动流程-CSDN博客文章浏览阅读1k次。SystemUI 是系统应用,由 SystemServer 进程进行启动,入口 Application 为SystemUIApplication。常用UI组件有如下几个:状态栏 StatusBar通知栏 NotificationPanel导航栏 NavigationBar最近任务 Recent键盘锁 Keyguard以上从 SystemUI 大概类图,以及自身启动流程开始,到 StatusBar 创建流程,再到系统 Notification 实现流程,一步步去理解 SystemUI 的相关流程。_systemui启动流程https://blog.csdn.net/Agg_bin/article/details/130252705

附录2:使用 NotificationListenerService 监听通知

Android9-W517-使用NotificationListenerService监听通知_android notificationlistenerservice-CSDN博客文章浏览阅读1.2k次,点赞18次,收藏15次。方案一通过Action跳转《系统设置》应用,手动打开通知监听权限:android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS——结果如图:显示在此设备上不能获得此特性——暂不可行;方案二在源码frameworks/base/core/res/res/values/config.xml路径下,修改config_defaultListenerAccessPackages属性的值为应用包名com.***.launcher——_android notificationlistenerservicehttps://blog.csdn.net/Agg_bin/article/details/136483571

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 我在IBM的时光碎片1 - 回忆昊海大厦
  • Unity (编辑器)数据持久化 之 ScriptableObject初识与创建
  • Adobe Illustrator vs Photoshop:设计界的“相声搭档”
  • 【类模板】模板参数的推断
  • [激光原理与应用-126]:傅里叶变化与频域分析
  • Redis的内存淘汰策略-volatile-ttl
  • 【Python机器学习】词向量推理——语义查询与类比
  • HarmonyOS实战开发:NAPI接口规范开发
  • 华为 HCIP-Datacom H12-821 题库 (3)
  • vscode Git代码版本回退
  • 【生日视频制作】宝马提车交车仪式感广告展示牌AE模板修改文字软件生成器教程特效素材【AE模板】
  • javacv-ffmpeg ProcessBuilder批量旋转图片
  • Elasticsearch Suggesters API详解与联想词自动补全应用
  • Oracle rac模式下undo表空间爆满的解决
  • 公钥密码选择题
  • [Vue CLI 3] 配置解析之 css.extract
  • CentOS从零开始部署Nodejs项目
  • egg(89)--egg之redis的发布和订阅
  • iOS 颜色设置看我就够了
  • python学习笔记-类对象的信息
  • React+TypeScript入门
  • Redis学习笔记 - pipline(流水线、管道)
  • select2 取值 遍历 设置默认值
  • Webpack 4x 之路 ( 四 )
  • 从零搭建Koa2 Server
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 聊聊flink的BlobWriter
  • 如何进阶一名有竞争力的程序员?
  • 使用agvtool更改app version/build
  • 详解NodeJs流之一
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • # 数仓建模:如何构建主题宽表模型?
  • #{}和${}的区别是什么 -- java面试
  • #define与typedef区别
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (+4)2.2UML建模图
  • (19)夹钳(用于送货)
  • (20)docke容器
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (ros//EnvironmentVariables)ros环境变量
  • (STM32笔记)九、RCC时钟树与时钟 第一部分
  • (二)十分简易快速 自己训练样本 opencv级联lbp分类器 车牌识别
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (九十四)函数和二维数组
  • (算法)前K大的和
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET Core 和 .NET Framework 中的 MEF2
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .NET 设计模式初探
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NET委托:一个关于C#的睡前故事