APP请求系统通知(“Notifications 通知”拆解)

APP请求系统通知(“Notifications 通知”拆解)

android12-release


APP创建Notification并请求

在这里插入图片描述

系统通知框架

在这里插入图片描述

  1. IBinder通信到
IBinder b = ServiceManager.getService("notification");
sService = INotificationManager.Stub.asInterface(b);
  1. 日志Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);

  2. channel == null:从 Android 8.0(API 级别 26)开始,必须为所有通知分配渠道,否则通知将不会显示。
    在这里插入图片描述

  3. NotificationRecord包含notification相关信息

  4. EnqueueNotificationRunnable/PostNotificationRunnable经过两道run()处理,最后NotificationListenerService通知监听显示

  5. 日志if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey());,这里正真通知添加队列,并输出Event日志EventLogTags.writeNotificationEnqueue
    在这里插入图片描述 在这里插入图片描述

  6. 调用NotificationListenerService实例对象的onNotificationPosted()
    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java
    在这里插入图片描述在这里插入图片描述

通知中振动/铃声/呼吸灯 notification_alert

应用请求系统通知在PostNotificationRunnable过程中buzzBeepBlinkLocked(r)方法处理振动/铃声/呼吸灯
在这里插入图片描述

  1. if (DBG) Slog.v(TAG, "Interrupting!");该日志表示将要开始处理振动/铃声
    在这里插入图片描述
  2. 呼吸灯处理
    在这里插入图片描述
  3. buzzBeepBlink = (buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0);应用请求了振动/铃声/呼吸灯输出notification_alert日志
    在这里插入图片描述
Notifications API 是一个允许网页和应用程序向用户发送通知的API。在Mac OS系统中,你可以使用该API来显示通知,这些通知会在Mac的右下角弹出,就像你收到邮件或者收到消息时系统弹出的提醒一样。为了使用 Notifications API,你需要在你的代码中调用相关的Web API接口。 以下是使用 Notifications API 创建通知的基本步骤: 1. 确保你的网页或者应用程序具有发送通知的权限。对于网页来说,通常需要向用户请求权限,可以使用 `Notification.requestPermission()` 方法。 2. 创建一个通知实例。你可以通过调用 `new Notification()` 构造函数来实现,并传递相应的参数,例如通知的标题、内容和选项等。 3. 你可以定义一些选项来自定义通知的外观和行为,比如声音、通知体的HTML内容、图标等。 下面是一个简单的JavaScript代码示例,展示了如何在Mac OS上使用Notifications API发送一个基本通知: ```javascript // 首先,检查通知权限是否已被授予 if (Notification.permission !== "granted") { // 请求通知权限 Notification.requestPermission().then(function(permission) { // 如果用户接受,则发送通知 if (permission === "granted") { sendNotification(); } }); } else { // 如果已有权限,则直接发送通知 sendNotification(); } function sendNotification() { // 创建一个通知对象 var notification = new Notification("新消息", { body: "你收到了一条新的通知。", icon: "path/to/icon.png" // 可选,通知图标 }); // 当通知被点击时触发的事件 notification.onclick = function() { // 这里可以定义点击通知后的行为,比如打开一个网页或应用程序的窗口 }; } ``` 使用Notifications API时,请注意以下几点: - 不同浏览器对于通知的支持和权限请求的实现可能会有所不同。 - 在某些浏览器中,通知的外观和行为可能会有所变化,以适应用户设置。 - 在移动设备上,通知的行为也可能与在桌面环境中的行为不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xhBruce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值