HarmonyOS Next: Notifications and reminders
Notification Kit provides developers with a local notification release channel, which allows developers to push notifications generated by applications directly to users on the client
1. Introduction to Notification Kit
Notification Kit provides developers with a local notification release channel, which allows developers to push notifications generated by applications directly to users on the client, and local notifications will generate corresponding ringtones, vibrations, banners, lock screens, screen offs, notification bar reminders, and displays according to the notification type and release scenario.
The application can send a notification message through the notification API, and the end user can view the notification content through the notification bar, or click the notification to open the application. Common scenarios for notification usage: Displays the events that are currently in progress, such as upload and download reminders Chat message alerts Displays app push messages
Note: If you are debugging an emulator, you need to enable the notification permission Open System Settings-
Open the notifications and status bar
Find your own app
Just allow notifications
Second, the scope of competence
The following capabilities are supported by Notification Kit: Publish notifications such as text, progress bars, and more. Carry or update the app notification digital badge. Cancel one or all of the notices that have been posted. Query the list of published notifications. Query the status of the application's own notifications. The ability to notify users is disabled by default, and developers can pull up the authorization box to request the user to authorize the release of notifications.
Third, there are also a variety of notification styles
There are text, multi-line text, notification large charts, notification corner charts, and progress bars
4. Notification is restrictive
The number of notifications published by a single app is limited (up to 24 notifications are currently available) at system portals such as Notification Center. The length of the notification cannot exceed 200KB (cross-process serialization size limit). The cumulative frequency of new notifications for all applications in the system cannot exceed 10 per second, and the cumulative frequency of update notifications cannot exceed 20 per second.
5. Development steps
Import the module
js
Code Interpretation
Copy Code
import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit';
Full code:
js
Code Interpretation
Copy Code
@Entry @Component struct NotificationPage { publishNotification() { // Notify the Request object let notificationRequest: notificationManager.NotificationRequest = { id: 1, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: "It's a notification bar.", text: "Current events ....", additionalText: "test_additionalText" } } }; notificationManager.publish(notificationRequest).then(() => { console.info("publish success"); }).catch((err: BusinessError) => { console.error(`publish fail: ${JSON.stringify(err)}`); }); } build() { Column() { Button('Send notification') .onClick(() => { this.publishNotification() }) } .width('100%') } }
Original source: HarmonyOS Next: Notifications and reminders (Juejin.cn)