Table of contents
2. Send ordinary text type notification
1. First initialize the broadcast request request
3. The display effect is as follows
3. Send long text type broadcast
1. Construct the parameter request for sending broadcast
3. The display effect is as follows
4. Send multi-line text type broadcast
1. Construct the parameter request for sending broadcast
5. Send picture type broadcast
2. Construct the parameter request for sending broadcast
1. Notification Overview
Introduction to Notifications
Apps can send notification messages through the notification interface, and end users can view the notification content through the notification bar, or click the notification to open the app.
Common usage scenarios for notifications:
-
Display received short messages, instant messages, etc.
-
Display push messages of the application, such as advertisements, version updates, etc.
-
Displays currently ongoing events, such as downloads, etc.
HarmonyOS manages notification-type messages through ANS (Advanced Notification Service, notification system service), and supports multiple notification types, such as basic type notifications and progress bar type notifications.
Notify business process
The notification business process consists of a notification subsystem, a notification sender, and a notification subscriber.
A notification is generated from the notification sender, sent to the notification subsystem through IPC communication, and then distributed to the notification subscriber by the notification subsystem.
The system application also supports notification-related configurations, such as enable switches and configuration parameters, which are requested by the system configuration and sent to the notification subsystem for storage in memory and database.
type of broadcast
- NOTIFICATION_CONTENT_BASIC_TEXT: normal text type
- NOTIFICATION_CONTENT_LONG_TEXT: Long text type
- NOTIFICATION_CONTENT_MULTILINE: multi-line text type
- NOTIFICATION_CONTENT_PICTURE: Picture type
The types of broadcasts are mainly divided into ordinary text types, which send ordinary text broadcasts; long text types, which send long text type broadcasts; multi-line text types, which can display text in multiple lines and send broadcasts; send picture type broadcasts.
Interface Description
The notification publishing interface is shown in the following table. Different publishing types of notifications carry different information in the fields of NotificationRequest.
interface name |
describe |
---|---|
publish(request: NotificationRequest, callback: AsyncCallback<void>): void |
Post a notice. |
cancel(id: number, label: string, callback: AsyncCallback<void>): void |
Cancels the specified notification. |
cancelAll(callback: AsyncCallback<void>): void; |
Suppresses all notifications from this app. |
Development preparation
Guide package
|
2. Send ordinary text type notification
1. First initialize the broadcast request request
|
2. Then send the broadcast
|
3. The display effect is as follows
Pull down the notification bar after clicking the send normal broadcast button
3. Send long text type broadcast
The long text type notification inherits the fields of the normal text type, and at the same time adds the long text content, content summary and title when the notification is expanded. The default display of the notification is the same as the normal text. After expanding, the title is displayed as the expanded title content, and the content is the long text content.
1. Construct the parameter request for sending broadcast
|
2. Then send the broadcast
|
3. The display effect is as follows
After clicking the button and then pulling down the notification bar to display the effect
Precautions
- The current test found that the long text should be long enough. If it is not long enough, only the long text content will be displayed, and the normal text content will not be displayed.
4. Send multi-line text type broadcast
The multi-line text type notification inherits the fields of the normal text type, and adds multi-line text content, content summary and title when the notification is expanded. The default display of the notification is the same as the normal text. After expanding, the title is displayed as the expanded title content, and the multi-line text content is displayed in multiple lines.
1. Construct the parameter request for sending broadcast
|
2. Send broadcast
|
3. Display effect
Pull down the notification bar after clicking
Precautions
- If there is only one line of text, only multi-line text content will be displayed, and normal text content will not be displayed
5. Send picture type broadcast
The image type notification inherits the fields of the ordinary text type, and adds the image content, content summary, and title when the notification is expanded. The image content is a PixelMap object, and its size cannot exceed 2M.
the code
|
display effect
After clicking the button, pull down the notification bar to display
6. Send intent type broadcast
Intent-type broadcasts are broadcasts that can be clicked and jump to the page after sending. Intent-type notifications inherit the normal text-type fields and add the wantAgent field. This parameter means which page to jump to
1. Create the wantAgent field
|
|
The data obtained above is the wantAgent parameter
2.构建发送广播的参数request
|
3.发送广播
|
4.显示结果
点击后下拉通知栏点击通知栏