HarmonyOS之深入解析通知的使用
一、简介
① 概念
- 通知提供应用的即时消息或通信消息,用户可以直接删除或点击通知触发进一步的操作。
- 通知目前支持六种样式:普通文本、长文本、图片、社交、多行文本和媒体样式。创建通知时必须包含一种样式。
- 通知支持快捷回复。
② 应用场景
- HarmonyOS 提供了通知功能,即在一个应用的 UI 界面之外显示的消息,主要用来提醒用户有来自该应用中的信息。当应用向系统发出通知时,它将先以图标的形式显示在通知栏中,用户可以下拉通知栏查看通知的详细信息。
- 常见的使用场景:
- 显示接收到短消息、即时消息等。
- 显示应用的推送消息,如广告、版本更新等。
- 显示当前正在进行的事件,如播放音乐、导航、下载等。
二、API 说明
- 通知相关基础类包含 NotificationSlot、NotificationRequest和NotificationHelper。基础类之间的关系如下所示:
① NotificationSlot
- NotificationSlot 可以对提示音、振动、锁屏显示和重要级别等进行设置。一个应用可以创建一个或多个 NotificationSlot,在发布通知时,通过绑定不同的 NotificationSlot,实现不同用途。
- NotificationSlot 需要先通过 NotificationHelper 的 addNotificationSlot(NotificationSlot) 方法发布后,通知才能绑定使用;所有绑定该 NotificationSlot 的通知在发布后都具备相应的特性,对象在创建后,将无法更改这些设置,对于是否启动相应设置,用户有最终控制权。
- 不指定 NotificationSlot 时,当前通知会使用默认的 NotificationSlot,默认的 NotificationSlot 优先级为 LEVEL_DEFAULT。
- NotificationSlot 主要接口:
接口名 | 描述 |
---|---|
NotificationSlot(String id, String name, int level) | 构造NotificationSlot |
setLevel(int level) | 设置NotificationSlot的级别 |
setName(String name) | 设置NotificationSlot的命名 |
setDescription(String description) | 设置NotificationSlot的描述信息 |
enableBypassDnd(boolean bypassDnd) | 设置是否绕过系统的免打扰模式 |
setEnableVibration(boolean vibration) | 设置收到通知时是否使能振动 |
setLockscreenVisibleness(int visibleness) | 设置在锁屏场景下,收到通知后是否显示,以及显示的效果 |
setEnableLight(boolean isLightEnabled) | 设置收到通知时是否开启呼吸灯,前提是当前硬件支持呼吸灯 |
setLedLightColor(int color) | 设置收到通知时的呼吸灯颜色 |
setSlotGroup(String groupId) | 绑定当前NotificationSlot到一个NotificationSlot组 |
- NotificationSlot 的级别目前支持如下几种, 由低到高:
- LEVEL_NONE: 表示通知不发布。
- LEVEL_MIN:表示通知可以发布,但是不显示在通知栏,不自动弹出,无提示音;该级别不适用于前台服务的场景。
- LEVEL_LOW:表示通知可以发布且显示在通知栏,不自动弹出,无提示音。
- LEVEL_DEFAULT:表示通知发布后可在通知栏显示,不自动弹出,触发提示音。
- LEVEL_HIGH:表示通知发布后可在通知栏显示,自动弹出,触发提示音。
② NotificationRequest
- NotificationRequest 用于设置具体的通知对象,包括设置通知的属性,如:通知的分发时间、小图标、大图标、自动删除等参数,以及设置具体的通知类型,如普通文本、长文本等。
- NotificationRequest 主要接口:
接口名 | 描述 |
---|---|
NotificationRequest() | 构建一个通知 |
NotificationRequest(int notificationId) | 构建一个通知,指定通知的id。通知的Id在应用内容具有唯一性,如果不指定,默认为0 |
setNotificationId(int notificationId) | 设置当前通知id |
setAutoDeletedTime(long time) | 设置通知自动取消的时间戳 |
setContent(NotificationRequest.NotificationContent content) | 设置通知的具体内容 |
setDeliveryTime(long deliveryTime) | 设置通知分发的时间戳 |
setSlotId(String slotId) | 设置通知的NotificationSlot id |
setTapDismissed(boolean tapDismissed) | 设置通知在用户点击后是否自动取消 |
setLittleIcon(PixelMap smallIcon) | 设置通知的小图标,在通知左上角显示 |
setBigIcon(PixelMap bigIcon) | 设置通知的大图标,在通知的右边显示 |
setGroupValue(String groupValue) | 设置分组通知,相同分组的通知在通知栏显示时,将会折叠在一组应用中显示 |
addActionButton(NotificationActionButton actionButton) | 设置通知添加通知ActionButton |
setIntentAgent(IntentAgent agent) | 设置通知承载指定的IntentAgent,在通知中实现即将触发的事件 |
- 具体的通知类型:目前支持六种类型,包括普通文本 NotificationNormalContent、长文本 NotificationLongTextContent、图片 NotificationPictureContent、多行 NotificationMultiLineContent、社交 NotificationConversationalContent、媒体 NotificationMediaContent。
- 通知类型的主要接口:
类名 | 接口名 | 描述 |
---|---|---|
NotificationNormalContent | setTitle(String title) | 设置通知标题 |
NotificationNormalContent | setText(String text) | 设置通知内容 |
NotificationNormalContent | setAdditionalText(String additionalText) | 设置通知次要内容,是对通知内容的补充 |
NotificationPictureContent | setBriefText(String briefText) | 设置通知概要内容,是对通知内容的总结 |
NotificationPictureContent | setExpandedTitle(String expandedTitle) | 设置附加图片的通知展开时的标题 |
NotificationPictureContent | setBigPicture(PixelMap bigPicture) | 设置通知的图片内容,附加在setText(String text)下方 |
NotificationLongTextContent | setLongText(String longText) | 设置通知的长文本 |
NotificationConversationalContent | setConversationTitle(String conversationTitle) | 设置社交通知的标题 |
NotificationConversationalContent | addConversationalMessage(ConversationalMessage message) | 通知添加一条消息 |
NotificationMultiLineContent | addSingleLine(String line) | 在当前通知中添加一行文本 |
NotificationMediaContent | setAVToken(AVToken avToken) | 将媒体通知绑定指定的AVToken |
NotificationMediaContent | setShownActions(int[] actions) | 设置媒体通知待展示的按钮 |
- 通知发布后,通知的设置不可修改。如果下次发布通知使用相同的 id,就会更新之前发布的通知。
③ NotificationHelper
- NotificationHelper 封装了发布、更新、删除通知等静态方法。
- NotificationHelper 主要接口:
接口名 | 描述 |
---|---|
publishNotification(NotificationRequest request) | 发布一条通知 |
publishNotification(String tag, NotificationRequest request) | 发布一条带TAG的通知 |
cancelNotification(int notificationId) | 取消指定的通知 |
cancelNotification(String tag, int notificationId) | 取消指定的带TAG的通知 |
cancelAllNotifications() | 取消之前发布的所有通知 |
addNotificationSlot(NotificationSlot slot) | 创建一个NotificationSlot |
getNotificationSlot(String slotId) | 获取NotificationSlot |
removeNotificationSlot(String slotId) | 删除一个NotificationSlot |
getActiveNotifications() | 获取当前应用发的活跃通知 |
getActiveNotificationNums() | 获取系统中当前应用发的活跃通知的数量 |
setNotificationBadgeNum(int num) | 设置通知的角标 |
setNotificationBadgeNum() | 设置当前应用中活跃状态通知的数量在角标显示 |
三、通知的使用
① 创建 NotificationSlot
- NotificationSlot 可以设置公共通知的震动,锁屏模式,重要级别等,并通过调用 NotificationHelper.addNotificationSlot() 发布 NotificationSlot 对象。
NotificationSlot slot = new NotificationSlot("slot_001", "slot_default", NotificationSlot.LEVEL_MIN); // 创建notificationSlot对象slot.setDescription("NotificationSlotDescription");slot.setEnableVibration(true); // 设置振动提醒slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);// 设置锁屏模式slot.setEnableLight(true); // 设置开启呼吸灯提醒slot.setLedLightColor(Color.RED.getValue());// 设置呼吸灯的提醒颜色try {NotificationHelper.addNotificationSlot(slot);} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during addNotificationSlot invocation.");}
② 发布通知
- 构建 NotificationRequest 对象,应用发布通知前,通过 NotificationRequest 的 setSlotId() 方法与 NotificationSlot 绑定,使该通知在发布后都具备该对象的特征。
int notificationId = 1;NotificationRequest request = new NotificationRequest(notificationId);request.setSlotId(slot.getId());
- 调用 setContent() 设置通知的内容:
String title = "title";String text = "There is a normal notification content.";NotificationNormalContent content = new NotificationNormalContent();content.setTitle(title).setText(text);NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);request.setContent(notificationContent); // 设置通知的内容
- 调用 publishNotification() 发布通知:
try {NotificationHelper.publishNotification(request);} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during publishNotification invocation.");}
③ 取消通知
- 取消通知分为取消指定单条通知和取消所有通知,应用只能取消自己发布的通知。
- 调用 cancelNotification() 取消指定的单条通知。
int notificationId = 1;try {NotificationHelper.cancelNotification(notificationId);} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during cancelNotification invocation.");}
- 调用 cancelAllNotifications() 取消所有通知:
try {NotificationHelper.cancelAllNotifications();} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during cancelAllNotifications invocation.");}
HarmonyOS之深入解析通知的使用相关推荐
- HarmonyOS之深入解析服务卡片的使用
一.概述 ① 基本概念 服务卡片(以下简称"卡片")是 FA 的一种界面展示形式,将 FA 的重要信息或操作前置到卡片,以达到服务直达,减少体验层级的目的. 卡片常用于嵌入到其他应 ...
- HarmonyOS之深入解析视频的功能和使用
一.基本概念 HarmonyOS 视频模块支持视频业务的开发和生态开放,开发者可以通过已开放的接口很容易地实现视频媒体的播放.操作和新功能开发. 视频媒体的常见操作有视频编解码.视频合成.视频提取.视 ...
- HarmonyOS之深入解析Ability的功能和使用
一.Ability 概述 Ability 是应用所具备能力的抽象,也是应用程序的重要组成部分.一个应用可以具备多种能力(即可以包含多个 Ability),HarmonyOS 支持应用以 Ability ...
- HarmonyOS之深入解析线程间的通信
一.概述 ① 基本概念 在开发过程中,开经常需要在当前线程中处理下载任务等较为耗时的操作,但是又不希望当前的线程受到阻塞.此时,就可以使用 EventHandler 机制. EventHandler ...
- HarmonyOS之深入解析蓝牙Bluetooth的功能和使用
一.蓝牙简介 ① 概念 蓝牙是短距离无线通信的一种方式,支持蓝牙的两个设备必须配对后才能通信.HarmonyOS 蓝牙主要分为传统蓝牙和低功耗蓝牙(通常称为 BLE,Bluetooth Low Ene ...
- HarmonyOS之深入解析WLAN的功能和使用
一.WLAN 简介 无线局域网(Wireless Local Area Networks,WLAN),是通过无线电.红外光信号或者其他技术发送和接收数据的局域网,用户可以通过 WLAN 实现结点之间无 ...
- HarmonyOS之深入解析NFC的功能和使用
一.简介 NFC(Near Field Communication,近距离无线通信技术) 是一种非接触式识别和互联技术,让移动设备.消费类电子产品.PC 和智能设备之间可以进行近距离无线通信. Har ...
- HarmonyOS之深入解析自定义组件与布局的实现
一.概述 HarmonyOS 提供了一套复杂且强大的 Java UI 框架,其中 Component 提供内容显示,是界面中所有组件的基类.ComponentContainer 作为容器容纳 Comp ...
- HarmonyOS之深入解析相机的功能和使用
一.简介 ① 概念 HarmonyOS 相机模块支持相机业务的开发,开发者可以通过已开放的接口实现相机硬件的访问.操作和新功能开发,最常见的操作如:预览.拍照.连拍和录像等. 相机静态能力:用于描述相 ...
最新文章
- summit_Chrome Dev Summit 2018的亮点
- Django操作原生MySQL的方法:MyModel.objects.raw()执行查询语句
- php 判断分辨率做判断,PHP判断是否是成人照片或者裸照:基于皮肤像素点来检测图片裸照的类文件...
- Java学习笔记之:Java 继承
- python求职者的建议_Python 求职和建议-从认识自己出发
- CISCO路由器连接ADSL之PPPoE配置
- ArcGIS锁定显示比例
- ie11兼容性问题,jsp在IE11显示不全问题,ie11覆盖内容问题解决方法
- MapGuide Maestro 3.0发布
- 富爸爸穷爸爸-读书笔记
- Nacos 服务治理(服务注册中心)
- 网络协议(一) TCP/IP 协议
- 使用BasicExcel操作Excel
- 护肤品html作业,聚美优品美容产品热点.html
- Java程序验证五子棋先手必胜,五子棋怎样下最厉害_五子棋先手必胜开局图
- linux服务器的Gzip文件压缩方法
- 1500个用户代理User agent,用于随机UA代理
- 【摘】WebGIS开发从入门到......
- 基于飞凌RK3588核心板的无人机主控方案
- mysql如何分表_MySQL分表和分区的具体实现方法
热门文章
- 同居1月 VS 同居1年,太真实了...
- 54. Spiral Matrix
- 分享个INNO打包Windows应用程序完整实例脚本(转载)
- 【转】犹太人的10句话,每一句都值得深思
- 提升PHP性能的21种方法
- python列表索引超出范围 等于啥_python中的“列表索引超出范围”
- C语言函数题-利用指针,实现字符串比较函数
- java画个半径为1地圆_java - 绘制一个半径为圆的圆并围绕边缘指向 - 堆栈内存溢出...
- jsp复选框追少选择一个否则不能提交_Filecoin的惩罚机制/至联云IPFS:选择无良矿商,你的抵押币就回不来了...
- r语言直方图_R语言绘制频率直方图