当前位置:网站首页>#夏日挑战赛# 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开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
边栏推荐
- Isprs2022/ cloud detection: cloud detection with boundary nets
- [HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
- 小程序 修改样式 ( placeholder、checkbox的样式)
- Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
- 如何写出好代码 - 防御式编程
- Linear table - abstract data type
- Interprocess communication (IPC): shared memory
- 蚂蚁集团开源可信隐私计算框架「隐语」:开放、通用
- uniapp获取微信头像和昵称
- XML basic knowledge concept
猜你喜欢
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
vs2017 qt的各种坑
Overview of video self supervised learning
AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources
MYSQL中 find_in_set() 函数用法详解
企业数字化转型之路,从这里开始
5年经验Android程序员面试27天,2022程序员进阶宝典
1亿单身男女撑起一个IPO,估值130亿
【Autosar 十四 启动流程详解】
集合处理的利器
随机推荐
进程间通信(IPC):共享内存
Tianyi cloud understands enterprise level data security in this way
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
为什么 BI 软件都搞不定关联分析?带你分析分析
[HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
IDEA配置npm启动
视频自监督学习综述
[detailed explanation of AUTOSAR 14 startup process]
UDF implementation of Dameng database
Why can't Bi software do correlation analysis? Take you to analyze
企业数字化转型之路,从这里开始
一文读懂简单查询代价估算
CDB 实例的启动与关闭
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
The easycvr authorization expiration page cannot be logged in. How to solve it?
uniapp获取微信头像和昵称
c期末复习
案例分享|金融业数据运营运维一体化建设
图扑软件数字孪生智慧风电系统
R language Visual scatter plot graph, add labels to some data points in the graph, and always display all labels, even if they have too much overlap. Ggrep package helps