当前位置:网站首页>#夏日挑战赛# 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开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
边栏推荐
- EMQX 5.0 正式发布:单集群支持 1 亿 MQTT 连接
- 2022最新大厂Android面试真题解析,Android开发必会技术
- 7-2 保持链表有序
- 中文版Postman?功能真心强大!
- 瞅一瞅JUC提供的限流工具Semaphore
- Oracle Chinese sorting Oracle Chinese field sorting
- Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换
- Various pits of vs2017 QT
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- Tupu software digital twin smart wind power system
猜你喜欢

5年经验Android程序员面试27天,2022程序员进阶宝典

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

Mysql database indexing tutorial (super detailed)

5. 数据访问 - EntityFramework集成

决策树与随机森林

视频自监督学习综述

【Autosar 十四 启动流程详解】

Tianyi cloud understands enterprise level data security in this way

Why can't Bi software do correlation analysis? Take you to analyze

How to write good code defensive programming
随机推荐
Precautions for RTD temperature measurement of max31865 module
The monthly list of Tencent cloud developer community videos was released in May 2022
Word finds red text word finds color font word finds highlighted formatted text
Benefits of automated testing
[detailed explanation of AUTOSAR 14 startup process]
SAP feature description
什么是文本挖掘 ?「建议收藏」
Why can't Bi software do correlation analysis? Take you to analyze
如何写出好代码 - 防御式编程
C final review
Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接
Windows Oracle open remote connection Windows Server Oracle open remote connection
2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
达梦数据库udf实现
A cloud opens a new future of smart transportation
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
Take a look at semaphore, the current limiting tool provided by JUC
彻底理解为什么网络 I/O 会被阻塞?
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout