当前位置:网站首页>#夏日挑战赛# HarmonyOS - 实现消息通知功能
#夏日挑战赛# HarmonyOS - 实现消息通知功能
2022-07-05 18:44:00 【51CTO】
作者:张明伟
前言
通知是手机软件的消息推送,一般需要设置通知的权限为允许通知才能在状态栏查看到通知。主要有以下使用场景:
app内的通知:如微信新消息的提醒,以及一些APP广告的推送,APP版本更新;
系统的通知,如电量过低,短信提醒等;
显示正在进行的事件,如音乐播放,下载等都是通知。
效果展示

实现步骤
1. 定义触发通知的事件
1.1 首先需要定义UI
(一般情况下,不需要UI,本实例为了能方便获取触发事件而定义UI)
1.2 实现JS FA调用PA的逻辑,并实现点击事件
2. 实现通知的逻辑
2.1 实现onRemoteRequest()方法
在工程中新建一个InternalAbility继承自AceInternalAbility,实现onRemoteRequest()方法
2.2 在MainAbility中注册与取消注册
2.3 通知开发步骤
通知相关基础类包含NotificationSlot、NotificationRequest和NotificationHelper。
NotificationSlot可以对提示音、振动、重要级别等进行设置。一个应用可以创建一个或多个NotificationSlot,在发布通知时,通过绑定不同的NotificationSlot,实现不同用途。NotificationRequest用于设置具体的通知对象,包括设置通知的属性,如:通知的分发时间、小图标、大图标、自动删除等参数,以及设置具体的通知类型,如普通文本、长文本等。NotificationHelper封装了发布、更新、删除通知等静态方法。在这里主要通过介绍文本消息通知和图片消息通知。
2.3.1 定义通知类型并设置基本属性内容
设置文本通知的头部文本,通知标题,通知的内容。
设置图片通知的头部文本,通知标题,通知的简短介绍,通知图片。
2.3.2 定义通知的响应按钮
如果响应的按钮为文本则需要设置builder的第一个参数为null,若响应的按钮为图片则需要设置builder的第一个参数为PixelMap对象
2.3.3 NotificationRequest设置
通过NotificationRequest对象对消息进行封装,设置通知内容,id以及回复按钮。
2.3.4 发布通知
(发布通知后手机状态栏会有通知信息显示)
通过调用NotificationHelper的publishNotification(NotificationRequest notificationRequest)
2.3.5 取消通知
(取消通知后通知会从手机状态栏消失)
通过调用NotificationHelper的cancelNotification(notification id)方法来实现,通过notificationid来辨别通知。
2.3.6 其他功能
若想对通知的提示音,振动,重要级别等进行设置,需要用到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) | 设置收到通知时是否使能振动。 |
| setEnableLight(boolean isLightEnabled) | 设置收到通知时是否开启呼吸灯,前提是当前硬件支持呼吸灯。 |
| setLedLightColor(int color) | 设置收到通知时的呼吸灯颜色。 |
注意:这个对象只有在真机上才有真实效果。
总结
以上就是开发一个消息通知的完整过程,对于消息通知的应用是一个APP必不可少的部分,是APP与用户交互的一个通道。由于刚开始接触鸿蒙,还有许多表述不恰当的地方,请多指正!
更多原创内容请关注: 中软国际 HarmonyOS 技术团队
入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
边栏推荐
- CDB 实例的启动与关闭
- 什么是文本挖掘 ?「建议收藏」
- The monthly list of Tencent cloud developer community videos was released in May 2022
- c语言简便实现链表增删改查「建议收藏」
- Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换
- Summary of six points of MySQL optimization
- 中文版Postman?功能真心强大!
- 瞅一瞅JUC提供的限流工具Semaphore
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- Thoroughly understand why network i/o is blocked?
猜你喜欢

面试官:Redis 过期删除策略和内存淘汰策略有什么区别?

Isprs2020/ cloud detection: transferring deep learning models for cloud detection between landsat-8 and proba-v

国内低代码开发平台靠谱的都有哪些?

Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition

案例分享|金融业数据运营运维一体化建设

小程序 修改样式 ( placeholder、checkbox的样式)

How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers

You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!

Find in MySQL_ in_ Detailed explanation of set() function usage

中文版Postman?功能真心强大!
随机推荐
CDB 实例的启动与关闭
什么是文本挖掘 ?「建议收藏」
R语言使用lubridate包处理日期和时间数据实战
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
websocket 工具的使用
Common time complexity
Use QT to traverse JSON documents and search sub objects
Use of websocket tool
RedHat7.4配置yum软件仓库(RHEL7.4)
Shang Silicon Valley Shang preferred project tutorial release
中文版Postman?功能真心强大!
[detailed explanation of AUTOSAR 14 startup process]
2022最新Android面试笔试,一个安卓程序员的面试心得
Windows Oracle open remote connection Windows Server Oracle open remote connection
Take a look at semaphore, the current limiting tool provided by JUC
XML基础知识概念
Chinese postman? Really powerful!
华律网牵手观测云,上线系统全链路可观测平台
Rse2020/ cloud detection: accurate cloud detection of high-resolution remote sensing images based on weak supervision and deep learning
Applet modification style (placeholder, checkbox style)