当前位置:网站首页>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 :
边栏推荐
- Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
- S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
- 再忙不能忘安全
- Where is the operation of new bonds? Is it safer and more reliable to open an account
- Relationship between floating elements and parent and brother boxes
- Common - Hero Minesweeper
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- How to safely and quickly migrate from CentOS to openeuler
- IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
猜你喜欢
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Jvmrandom cannot set seeds | problem tracing | source code tracing
建立自己的网站(16)
95后阿里P7晒出工资单:狠补了这个,真香...
How about testing outsourcing companies?
What is the core value of testing?
如何在2022年更明智地应用智能合约?
[untitled]
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
Interviewer: what is the internal implementation of set data types in redis?
随机推荐
Tasks in GStreamer
redis集群模拟消息队列
手机股票开户安全吗?靠不靠谱啊?
2023年深圳市绿色低碳产业扶持计划申报指南
How to apply smart contracts more wisely in 2022?
c——顺序结构
测试外包公司怎么样?
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
C application interface development foundation - form control (5) - grouping control
Gstreamer中的task
Using repositoryprovider to simplify the value passing of parent-child components
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
太牛了,看这篇足矣了
No matter how busy you are, you can't forget safety
浮动元素与父级、兄弟盒子的关系
Fundamentals of shell programming (Part 8: branch statements -case in)
四万字长文说operator new & operator delete
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
安卓面试宝典,2022Android面试笔试总结
软件测试是干什么的?学习有啥要求?