HarmonyOS开发-通知开发体验

来源: 鸿蒙时代 作者:鸿蒙时代 2022-03-16 11:35:05

  HarmonyOS提供了通知功能,即在一个应用的UI界面之外显示的消息,主要用来提醒用户有来自该应用中的信息。当应用向系统发出通知时,它将先以图标的形式显示在通知栏中,用户可以下拉通知栏查看通知的详细信息。

  效果如下:

  代码如下:

private void NotificationSlot(){
    NotificationSlot slot = new NotificationSlot("slot_001", "slot_default", NotificationSlot.LEVEL_MIN); // 创建notificationSlot对象
    slot.setDescription("NotificationSlotDescription");
    slot.setEnableVibration(true); // 设置振动提醒
    slot.setEnableLight(true); // 设置开启呼吸灯提醒
    slot.setLedLightColor(Color.RED.getValue());// 设置呼吸灯的提醒颜色
    try {
        NotificationHelper.addNotificationSlot(slot);
    } catch (RemoteException ex) {
        HiLog.error((HiLogLabel) LABEL, "Exception occurred during addNotificationSlot invocation.");
    }

    int notificationId = 1;
    NotificationRequest request = new NotificationRequest(notificationId);
    request.setSlotId(slot.getId());

    String title = "消息";
    String text = "哈哈哈哈哈哈哈哈";
    NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
    content.setTitle(title)
            .setText(text);
    NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
    request.setContent(notificationContent); // 设置通知的内容

    try {
        NotificationHelper.publishNotification(request);
    } catch (RemoteException ex) {
        HiLog.error((HiLogLabel) LABEL, "Exception occurred during publishNotification invocation.");
    }
}
0
收藏
0