当前位置:网站首页>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 :
边栏推荐
- 集合
- Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
- 期货如何网上开户?安不安全?
- Add data to excel small and medium-sized cases through poi
- Flume series: interceptor filtering data
- 深度學習 卷積神經網絡(CNN)基礎
- JVMRandom不可设置种子|问题追溯|源码追溯
- 【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
- Fundamentals of deep learning convolutional neural network (CNN)
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
猜你喜欢
Do you know several assertion methods commonly used by JMeter?
浅浅的谈一下ThreadLocalInsecureRandom
redis集群模拟消息队列
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Reflection and imagination on the notation like tool
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
再忙不能忘安全
大厂面试必备技能,2022Android不死我不倒
14. Users, groups, and permissions (14)
随机推荐
软件测试工程师是做什么的?待遇前景怎么样?
期货如何网上开户?安不安全?
安信证券在网上开户安全吗?
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
四万字长文说operator new & operator delete
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Where is the operation of new bonds? Is it safer and more reliable to open an account
什么是面上项目
通过POI追加数据到excel中小案例
MMO project learning 1: preheating
Using repositoryprovider to simplify the value passing of parent-child components
2023年深圳市绿色低碳产业扶持计划申报指南
【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
安卓面试宝典,2022Android面试笔试总结
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
【obs】libobs-winrt :CreateDispatcherQueueController
Add data to excel small and medium-sized cases through poi
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤