当前位置:网站首页>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 :
边栏推荐
- Common - Hero Minesweeper
- Gstreamer中的task
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- Redis cluster simulated message queue
- 成功入职百度月薪35K,2022Android开发面试解答
- JMeter 常用的几种断言方法,你会了吗?
- Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
- Postman核心功能解析-参数化和测试报告
- Is it safe for Guosen Securities to open an account online?
- 大厂面试必备技能,2022Android不死我不倒
猜你喜欢

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

集合

Common - Hero Minesweeper

How about testing outsourcing companies?

【硬核干货】数据分析哪家强?选Pandas还是选SQL

Force buckle 1200 Minimum absolute difference
完爆面试官,一线互联网企业高级Android工程师面试题大全
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer

四万字长文说operator new & operator delete

Reflection and imagination on the notation like tool
随机推荐
The difference between ID selector and class selector
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
众昂矿业:2022年全球萤石行业市场供给现状分析
MMO项目学习一:预热
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
力扣 1200. 最小绝对差
-v parameter of GST launch
MMO project learning 1: preheating
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
How about testing outsourcing companies?
【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
[AI framework basic technology] automatic derivation mechanism (autograd)
C#应用程序界面开发基础——窗体控制(5)——分组类控件
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
What does software testing do? What are the requirements for learning?
Common operators and operator priority
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value