当前位置:网站首页>Summer Challenge harmonyos - realize message notification function
Summer Challenge harmonyos - realize message notification function
2022-07-05 19:52:00 【51CTO】
author : Zhang Mingwei
This article is participating in the starlight project 3.0– Summer Challenge
Preface
Notification is the message push of mobile software , Generally, you need to set the permission of notification to allow notification to view the notification in the status bar . There are mainly the following usage scenarios :
app Notification within : Such as the reminder of wechat new news , As well as some APP Advertising push ,APP Version update ;
System notification , If the power is too low , Text messages, reminders, etc ;
Show ongoing events , Like music playing , Downloading and so on are all notices .
Effect display

Implementation steps
1. Define the event that triggers the notification
1.1 First, we need to define UI
( In general , Unwanted UI, This example defines UI)
1.2 Realization JS FA call PA The logic of , And implement click events
2. Realize the logic of notification
2.1 Realization onRemoteRequest() Method
Build a new one in the project InternalAbility Inherited from AceInternalAbility, Realization onRemoteRequest() Method
2.2 stay MainAbility Register and cancel registration
2.3 Inform the development steps
Notice that the underlying class contains NotificationSlot、NotificationRequest and NotificationHelper.
NotificationSlot The prompt tone can be adjusted 、 Vibration 、 Set the importance level . An application can create one or more NotificationSlot, At the time of the announcement , By binding different NotificationSlot, For different purposes .NotificationRequest Used to set specific notification objects , This includes setting the properties of the notification , Such as : The distribution time of the notice 、 Small icons 、 Large icon 、 Automatically delete and other parameters , And setting specific notification types , Like plain text 、 Long text, etc .NotificationHelper Encapsulates the release 、 to update 、 Static methods such as delete notification . Here we mainly introduce text message notification and picture message notification .
2.3.1 Define the notification type and set the basic attribute content
Set the header text of the text notification , Notification heading , Contents of the notice .
Set the header text of the picture notification , Notification heading , Brief introduction of the notice , Notice picture .
2.3.2 Define the response button for the notification
If the response button is text, you need to set builder The first parameter of is null, If the response button is an image, you need to set builder The first parameter of is PixelMap object
2.3.3 NotificationRequest Set up
adopt NotificationRequest Object encapsulates the message , Set notification content ,id And the reply button .
2.3.4 Issue notice
( After the notification is released, the status bar of the mobile phone will display the notification information )
By calling NotificationHelper Of publishNotification(NotificationRequest notificationRequest)
2.3.5 Notice of cancellation
( After canceling the notification, the notification will disappear from the mobile phone status bar )
By calling NotificationHelper Of cancelNotification(notification id) Method to implement , adopt notificationid To identify the notice .
2.3.6 Other features
If you want the prompt tone of the notice , Vibration , Set the importance level , Need to use NotificationSlot object , You need to set it before publishing .
Its main interfaces are shown in the table below .
| The interface name | describe |
|---|---|
| NotificationSlot(String id, String name, int level) | structure NotificationSlot. |
| setLevel(int level) | Set up NotificationSlot The level of . |
| setName(String name) | Set up NotificationSlot The name of . |
| setDescription(String description) | Set up NotificationSlot Description information of . |
| enableBypassDnd(boolean bypassDnd) | Set whether to bypass the undisturbed mode of the system . |
| setEnableVibration(boolean vibration) | Set whether to enable vibration when receiving notification . |
| setEnableLight(boolean isLightEnabled) | Set whether to turn on the breathing light when receiving the notification , The premise is that the current hardware supports breathing lamps . |
| setLedLightColor(int color) | Set the color of the breathing light when receiving the notification . |
Be careful : This object has real effect only on real machine .
summary
The above is the whole process of developing a message notification , The application of message notification is a APP The essential part , yes APP A channel to interact with users . Because I just started to contact Hongmeng , There are many inappropriate expressions , Please correct more !
For more original content, please pay attention to : China soft International HarmonyOS Technical team
Beginner to master 、 Skills to cases , Systematic sharing HarmonyOS Development technology , Welcome to contribute and subscribe , Let's move forward hand in hand to build Hongmeng ecology .
Want to know more about open source , Please visit :
边栏推荐
- Wildcard selector
- [C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
- MMO項目學習一:預熱
- 建立自己的网站(16)
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- Add data to excel small and medium-sized cases through poi
- 太牛了,看这篇足矣了
- Concept and syntax of function
- 力扣 729. 我的日程安排表 I
- 【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
猜你喜欢
![[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform](/img/64/08faef0fccec93337f0716ac57dd8e.jpg)
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform

The city chain technology Digital Innovation Strategy Summit was successfully held
![[AI framework basic technology] automatic derivation mechanism (autograd)](/img/9c/a5713def131dc7643cc19b3839ff0c.png)
[AI framework basic technology] automatic derivation mechanism (autograd)

Reinforcement learning - learning notes 4 | actor critical

众昂矿业:2022年全球萤石行业市场供给现状分析

Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)

浅浅的谈一下ThreadLocalInsecureRandom

Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022

C#应用程序界面开发基础——窗体控制(5)——分组类控件

MMO项目学习一:预热
随机推荐
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
MMO項目學習一:預熱
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
MMO项目学习一:预热
Fundamentals of shell programming (Chapter 9: loop)
集合
Is the education of caiqiantang reliable and safe?
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
期货如何网上开户?安不安全?
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
How about testing outsourcing companies?
城链科技数字化创新战略峰会圆满召开
【硬核干货】数据分析哪家强?选Pandas还是选SQL
Flume series: interceptor filtering data
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
No matter how busy you are, you can't forget safety
What do software test engineers do? How about the prospect of treatment?
Debezium series: PostgreSQL loads the correct last submission LSN from the offset