Skip to content

基础通知

基础通知可以通过通知接口发送通知信息,提醒用户关注应用中的变化.用户可以在通知栏查看和操作通知内容

流程

导入 notification 模块

js
import { notificationManager } from "@kit.NotificationKit";

发送通知(最简单的)

  • 第一步引入

  • 第二步创建发的通知

  • 第三步发送通知

js
import { notificationManager } from '@kit.NotificationKit';

sendNormalText() {
    console.log("确实点击了")
    let request: notificationManager.NotificationRequest = {
      id: 10,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: '通知标题',
          text: '通知详情',
          additionalText: "通知附加内容"
        }
      }
    }
    notificationManager.publish(request).then(()=>{console.log("发送通知")}).catch((err)=>{
      console.log("发送失败",JSON.stringify(err))
    })
}

Button("发送normalText通知")
  .type(ButtonType.Capsule)
  .onClick(() => {
    this.sendNormalText();
  });

图片

取消通知

  • 取消指定 id 的通知
bash

 // 取消指定id的通知
  notificationManager.cancel(10)
  • 取消所有当前应用的通知
bash

// 取消当前所有通知
notificationManager.cancelAll()

通知类型

类型枚举说明
NOTIFICATION_CONTENT_BASIC_TEXT普通文本型
NOTIFICATION_CONTENT_LONG_TEXT长文本型
NOTIFICATION_CONTENT_PICTURE图片型
NOTIFICATION_CONTENT_MULTILINE多行文本型

普通文本类型

js
import notificationManager from '@ohos.notificationManager';
sendNormalText() {
    let request: notificationManager.NotificationRequest = {
      id: 10,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: '通知标题',
          text: '通知详情',
          additionalText: "通知附加内容"
        }
      }
    }
    notificationManager.publish(request).then(() => {
      console.log("发送通知")
    }).catch((err) => {
      console.log("发送失败", JSON.stringify(err))
    })
  }
// 调用
 Button("发送normalText通知").type(ButtonType.Capsule).onClick(() => {
          this.sendNormalText()
        })

普通文本

长文本类型

js

import { notificationManager } from '@kit.NotificationKit';

sendLongText() {
    let request: notificationManager.NotificationRequest = {
      id: 10,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
        longText: {
          title: '通知标题',
          text: '通知详情',
          additionalText: "通知附加内容",
          longText: "通知中的长文本,我很长我很长我很长我很长我很长我很长",
          expandedTitle: "通知展开时候的标题",
          briefText: "通知概要总结" // 在这里不会显示
        }
      }
    }
    notificationManager.publish(request).then(() => {
      console.log("发送通知")
    }).catch((err) => {
      console.log("发送失败", JSON.stringify(err))
    })
  }

// 调用
Button("发送longText通知").type(ButtonType.Capsule).onClick(() => {
          this.sendLongText()
        })

普通文本

多行文本类型

js
import { notificationManager } from '@kit.NotificationKit';
  sendMultiLine() {
    let request: notificationManager.NotificationRequest = {
      id: 10,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,
        multiLine: {
          title: '通知标题',
          text: '通知详情',
          additionalText: "通知附加内容",
          briefText: "通知概要总结", // 在这里不会显示
          longTitle: "展开的标题,有多行,我很宽",
          lines: [
            '第一行',
            "第二行",
            "第三行",
            "第四行"
          ]
        }
      }
    }
    notificationManager.publish(request).then(() => {
      console.log("发送通知")
    }).catch((err) => {
      console.log("发送失败", JSON.stringify(err))
    })
  }
 Button("发送multiLine通知").type(ButtonType.Capsule).onClick(() => {
          this.sendMultiLine()
        })

普通文本

图片类型

js
import { notificationManager } from '@kit.NotificationKit';
import image from "@ohos.multimedia.image";
@Entry
@Component
struct SecondPage {
  @State message: string = 'Hello World'
  pixel: PixelMap
  async aboutToAppear() {
    // 获取资源管理器
    let rm = getContext(this).resourceManager;
    // 读取图片
    let file = await rm.getMediaContent($r("app.media.startIcon"))
    // 创建pixelMap
    image.createImageSource(file.buffer).createPixelMap().then((value) => this.pixel = value).catch((reason) => {
      console.log('testTag', '加载图片异常', JSON.stringify(reason))
    })
  }
 // 发送图片类型通知
  sendPicture() {
    let request: notificationManager.NotificationRequest = {
      id: 10,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
        picture: {
          title: "通知标题",
          text: '通知内容详情',
          additionalText: '通知附加内容',
          briefText: '通知概要和总结',
          expandedTitle: "展开后标题",
          picture: this.pixel
        }
      }
    }
    notificationManager.publish(request).then(() => {
      console.log("发送通知")
    }).catch((err) => {
      console.log("发送失败", JSON.stringify(err))
    })
  }
// 调用
 Button("发送Picture通知").type(ButtonType.Capsule).onClick(() => {
          this.sendPicture()
    })
}

普通文本

其他参数

js
let request: notificationManager.NotificationRequest = {
  id: 10,
  content: {
    contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
    picture: {
      title: "通知标题",
      text: "通知内容详情",
      additionalText: "通知附加内容",
      briefText: "通知概要和总结",
      expandedTitle: "展开后标题",
      picture: this.pixel,
    },
  },
  deliveryTime: new Date().getTime(),
  showDeliveryTime: true,
  groupName: "xxx",
  slotType: notify.SlotType.SOCIAL_COMMUNICATION,
};
  • deliveryTime 主要是用来设置时间

  • showDeliveryTime"用来显示时间

  • groupName 用来设置分组

slotType 四种类型

类型枚举说明状态栏图标提示音横幅
SOCIAL_COMMUNICATION社交类型
SERVICE_INFORMATION服务类型
CONTENT_INFORMATION内容类型
OTHER_TYPES其它

图片

进度条模式

进度条通知会展示一个动态的进度条,主要用于文件的下载,长任务处理的进度显示

判断当前系统是否支持进度条

    1. 判断当前系统是否支持进度条模板
js
this.isSupport = await notificationManager.isSupportTemplate(
  "downloadTemplate"
);

if (!this.isSupport) {
  return;
}
  • 2.定义通知请求
js
// 1.通知模板

let template = {
  name: "downloadTemplate", // 模板名称,不能更改必须是downloadTemplate
  data: {
    progressValue: this.progressValue, // 进度条当前进度
    progressMaxValue: 100, // 进度条最大值
  },
};

// 2. 通知请求

let request: notificationManager.NotificationRequest = {
  id: 999,
  template: template,
  content: {
    contentType:
      notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    normal: {
      title: "通知标题",
      text: "通知详情",
      additionalText: "通知附加内容",
    },
  },
};

notificationManager
  .publish(request)
  .then(() => {
    console.log("发送通知");
  })
  .catch((err) => {
    console.log("发送失败", JSON.stringify(err));
  });

图片

INFO

这里的进度条通知必须每次进度改变的时候发一次。否则一直是开始的数值