当前位置:网站首页>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 :
边栏推荐
- Base du réseau neuronal de convolution d'apprentissage profond (CNN)
- Necessary skills for interview in large factories, 2022android will not die, I will not fall
- Interviewer: what is the internal implementation of set data types in redis?
- How to safely and quickly migrate from CentOS to openeuler
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
- JMeter 常用的几种断言方法,你会了吗?
- Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
- That's awesome. It's enough to read this article
- No matter how busy you are, you can't forget safety
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
猜你喜欢

测试外包公司怎么样?

秋招字节面试官问你还有什么问题?其实你已经踩雷了
![[AI framework basic technology] automatic derivation mechanism (autograd)](/img/9c/a5713def131dc7643cc19b3839ff0c.png)
[AI framework basic technology] automatic derivation mechanism (autograd)

The city chain technology Digital Innovation Strategy Summit was successfully held

【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*

leetcode刷题:二叉树18(最大二叉树)

Summer Challenge database Xueba notes, quick review of exams / interviews~

webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
![[untitled]](/img/51/c89d35c855e299b02137d676790eb6.png)
[untitled]
![[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
随机推荐
Let's talk about threadlocalinsecurerandom
大厂面试必备技能,2022Android不死我不倒
爬虫练习题(二)
What is the core value of testing?
Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
redis集群模拟消息队列
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
leetcode刷题:二叉树18(最大二叉树)
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
Force buckle 1200 Minimum absolute difference
Fundamentals of shell programming (Part 8: branch statements -case in)
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
挖财钱堂教育靠谱安全吗?
-v parameter of GST launch
股票开户哪里好?网上客户经理开户安全吗
Android interview, Android audio and video development
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
40000 word Wenshuo operator new & operator delete
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
Where is the operation of new bonds? Is it safer and more reliable to open an account