发布基础类型通知

2024-02-16 13:50 更新

基础类型通知主要应用于发送短信息、提示信息、广告推送等,支持普通文本类型、长文本类型、多行文本类型和图片类型。

表1 基础类型通知中的内容分类

类型

描述

NOTIFICATION_CONTENT_BASIC_TEXT

普通文本类型。

NOTIFICATION_CONTENT_LONG_TEXT

长文本类型。

NOTIFICATION_CONTENT_MULTILINE

多行文本类型。

NOTIFICATION_CONTENT_PICTURE

图片类型。

目前系统仅通知栏订阅了通知,将通知显示在通知栏里。基础类型通知呈现效果示意图如下所示。

说明

根据设计样式的不同,通知的实际显示效果可能有所差异。本文中所涉及的通知效果图仅供参考,请以实际运行结果为准。

图1 基础类型通知呈现效果示意图 

接口说明

通知发布接口如下表所示,不同发布类型通知由NotificationRequest的字段携带不同的信息。

接口名

描述

publish(request: NotificationRequest, callback: AsyncCallback<void>): void

发布通知。

cancel(id: number, label: string, callback: AsyncCallback<void>): void

取消指定的通知。

cancelAll(callback: AsyncCallback<void>): void;

取消所有该应用发布的通知。

开发步骤

  1. 导入模块。

    1. import NotificationManager from '@ohos.notificationManager';
  2. 构造NotificationRequest对象,并发布通知。

    • 普通文本类型通知由标题、文本内容和附加信息三个字段组成,其中标题和文本内容是必填字段。

      1. let notificationRequest = {
      2. id: 1,
      3. content: {
      4. contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
      5. normal: {
      6. title: 'test_title',
      7. text: 'test_text',
      8. additionalText: 'test_additionalText',
      9. }
      10. }
      11. }
      12. NotificationManager.publish(notificationRequest, (err) => {
      13. if (err) {
      14. console.error(`[ANS] failed to publish, error[${err}]`);
      15. return;
      16. }
      17. console.info(`[ANS] publish success`);
      18. });

      运行效果如下图所示。

    • 长文本类型通知继承了普通文本类型的字段,同时新增了长文本内容、内容概要和通知展开时的标题。通知默认显示与普通文本相同,展开后,标题显示为展开后标题内容,内容为长文本内容。

      1. let notificationRequest = {
      2. id: 1,
      3. content: {
      4. contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知
      5. longText: {
      6. title: 'test_title',
      7. text: 'test_text',
      8. additionalText: 'test_additionalText',
      9. longText: 'test_longText',
      10. briefText: 'test_briefText',
      11. expandedTitle: 'test_expandedTitle',
      12. }
      13. }
      14. }
      15. // 发布通知
      16. NotificationManager.publish(notificationRequest, (err) => {
      17. if (err) {
      18. console.error(`[ANS] failed to publish, error[${err}]`);
      19. return;
      20. }
      21. console.info(`[ANS] publish success`);
      22. });

      运行效果如下图所示。

    • 多行文本类型通知继承了普通文本类型的字段,同时新增了多行文本内容、内容概要和通知展开时的标题。通知默认显示与普通文本相同,展开后,标题显示为展开后标题内容,多行文本内容多行显示。

      1. let notificationRequest = {
      2. id: 1,
      3. content: {
      4. contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本类型通知
      5. multiLine: {
      6. title: 'test_title',
      7. text: 'test_text',
      8. briefText: 'test_briefText',
      9. longTitle: 'test_longTitle',
      10. lines: ['line_01', 'line_02', 'line_03', 'line_04'],
      11. }
      12. }
      13. }
      14. // 发布通知
      15. NotificationManager.publish(notificationRequest, (err) => {
      16. if (err) {
      17. console.error(`[ANS] failed to publish, error[${err}]`);
      18. return;
      19. }
      20. console.info(`[ANS] publish success`);
      21. });

      运行效果如下图所示。

    • 图片类型通知继承了普通文本类型的字段,同时新增了图片内容、内容概要和通知展开时的标题,图片内容为PixelMap型对象,其大小不能超过2M。

      1. let imagePixelMap: PixelMap = undefined; // 需要获取图片PixelMap信息
      2. let notificationRequest: notificationManager.NotificationRequest = {
      3. id: 1,
      4. content: {
      5. contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
      6. picture: {
      7. title: 'test_title',
      8. text: 'test_text',
      9. additionalText: 'test_additionalText',
      10. briefText: 'test_briefText',
      11. expandedTitle: 'test_expandedTitle',
      12. picture: imagePixelMap
      13. }
      14. }
      15. };
      16. // 发布通知
      17. notificationManager.publish(notificationRequest, (err) => {
      18. if (err) {
      19. console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
      20. return;
      21. }
      22. console.info('Succeeded in publishing notification.');
      23. });

      运行效果如下图所示。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号