“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

notice

HarmonyOS provides app notification, which notifies users of events outside the app by using the app icon. Common usage scenarios:

  • Displays the received SMS and IM.
  • Display push notifications for your app, such as ads, version updates, etc.
  • Displays the current ongoing events, such as playing music, navigation, downloading, and so on.

Interface specification

Notification related base classes include NotificationSlot, NotificationRequest, and NotificationHelper. The relationship between the base classes is as follows:

Figure 1 Notification base class diagram

  • NotificationSlot

NotificationSlot sets the tone, vibration, lock screen display, and importance level. An application can create one or more NotificationSlots, and bind different NotificationSlots for different purposes when publishing notifications.

The following levels of NotificationSlot are currently supported from lowest to highest:

  • LEVEL_NONE: Indicates that notifications are not published.
  • LEVEL_MIN: indicates that a notification can be published, but the notification is not displayed in the notification bar, is not automatically displayed, and there is no prompt tone. This level does not apply to scenarios with front desk services.
  • LEVEL_LOW: Indicates that the notification can be published and displayed in the notification bar. The notification is not automatically displayed and there is no prompt tone.
  • LEVEL_DEFAULT: indicates that after a notification is published, it can be displayed in the notification bar.
  • LEVEL_HIGH: Indicates that after a notification is published, it can be displayed in the notification bar and automatically pops up to trigger an announcement tone.
  • NotificationRequest

NotificationRequest is used to set the notification object, including setting the notification properties, such as the notification distribution time, small icon, large icon, and automatic deletion parameters, and setting the notification type, such as plain text and long text.

Specific notification types: Currently six types are supported, Including ordinary text NotificationNormalContent, NotificationPictureContent NotificationLongTextContent long text, picture, multi-line NotificationMultiLineConte Nt, social NotificationConversationalContent NotificationMediaContent, media.

  • NotificationHelper

NotificationHelper encapsulates static methods such as publish, update, and delete notifications.

Results demonstrate

[video (video – 8 u5lpzne – 1625130916502) (type – CSDN) (url-live.csdn.net/v/embed/168…)”

Development steps

Notification development guidance is divided into the creation of NotificationSlot, publish notification, and cancel notification development scenarios.

Step 1: Initialize the NotificationSlot


    public static final String SLOT_ID = "high";
    public static final String SLOT_NAME = "Order notification";
//--------------------.@Override
public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_ability_slice); . defineNotificationSlot(Const.SLOT_ID, Const.SLOT_NAME, NotificationSlot.LEVEL_HIGH); . }//---------------------
private void defineNotificationSlot(String id, String name, int importance) {
        // Create notificationSlot object
        NotificationSlot notificationSlot = new NotificationSlot(id, name, importance);
        // Set vibration alert
        notificationSlot.setEnableVibration(true);
        // Set the screen lock mode
        notificationSlot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);
        Uri uri = Uri.parse(Const.SOUND_URI);
        notificationSlot.setSound(uri);
        try {
            NotificationHelper.addNotificationSlot(notificationSlot);
        } catch (RemoteException ex) {
            HiLog.error(LABEL_LOG, "%{public}s"."defineNotificationSlot remoteException."); }}Copy the code

Step 2: Issue a notice

private void publishNotification(String title, String text) {
       // Build the NotificationRequest object. Before applying the notification, bind the NotificationSlot with the setSlotId() method of NotificationRequest, and make the notification have the characteristics of the object after the notification is published
        notificationId = 0x1000001;
        NotificationRequest request = new NotificationRequest(notificationId).setSlotId(Const.SLOT_ID)
            .setTapDismissed(true);
        // Call setContent() to set the content of the notification
        request.setContent(createNotificationContent(title, text));
        IntentAgent intentAgent = createIntentAgent(MainAbility.class.getName(),
            IntentAgentConstant.OperationType.START_ABILITY);
        request.setIntentAgent(intentAgent);
        // Call publishNotification() to publish the notification
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException ex) {
            HiLog.error(LABEL_LOG, "%{public}s"."publishNotification remoteException."); }}Copy the code

Step 3: Cancel the notification

Cancellation notification can be divided into cancellation of a specific notification and cancellation of all notifications. The app can only cancel notifications published by itself.

  • Call cancelNotification() to cancel the specified single notification.
private void cancel(a) {
        try {
            NotificationHelper.cancelNotification(notificationId);
        } catch (RemoteException ex) {
            HiLog.error(LABEL_LOG, "%{public}s"."cancel remoteException."); }}Copy the code
  • Call cancelAllNotifications() to cancelAllNotifications
private void cancelAll(a) {
        try {
            NotificationHelper.cancelAllNotifications();
        } catch (RemoteException ex) {
            HiLog.error(LABEL_LOG, "%{public}s"."cancelAll remoteException."); }}Copy the code