基础类型通知主要应用在哪些方面?


HarmonyOS
2024-06-13 10:56:00
1139浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
dickhome

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

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

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

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

接口

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

开发步骤

1. 导入模块。

import NotificationManager from '@ohos.notificationManager';
  • 1.

2. 构造NotificationRequest对象,并发布通知。

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

运行效果如下图所示。

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

let notificationRequest = { 
  id: 1, 
  content: { 
    contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知 
    normal: { 
      title: 'test_title', 
      text: 'test_text', 
      additionalText: 'test_additionalText', 
    } 
  } 
} 
 
NotificationManager.publish(notificationRequest, (err) => { 
  if (err) { 
    console.error(`[ANS] failed to publish, error[${err}]`); 
    return; 
  } 
  console.info(`[ANS] publish success`); 
}); 
let notificationRequest = { 
  id: 1, 
  content: { 
    contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知 
    longText: { 
      title: 'test_title', 
      text: 'test_text', 
      additionalText: 'test_additionalText', 
      longText: 'test_longText', 
      briefText: 'test_briefText', 
      expandedTitle: 'test_expandedTitle', 
    } 
  } 
} 
// 发布通知 
NotificationManager.publish(notificationRequest, (err) => { 
  if (err) { 
    console.error(`[ANS] failed to publish, error[${err}]`); 
    return; 
  } 
  console.info(`[ANS] publish success`); 
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

运行效果如下图所示。

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

// 图片构造 
const color = new ArrayBuffer(60000); 
let bufferArr = new Uint8Array(color); 
for (var i = 0; i < bufferArr.byteLength; i++) { 
  bufferArr[i++] = 60; 
  bufferArr[i++] = 20; 
  bufferArr[i++] = 220; 
  bufferArr[i] = 100; 
} 
let opts = { editable: true, pixelFormat: "ARGB_8888", size: { height: 100, width: 150 } }; 
await image.createPixelMap(color, opts).then(async (pixelmap) => { 
    await pixelmap.getImageInfo().then(imageInfo => { 
      console.log("=====size: ====" + JSON.stringify(imageInfo.size)); 
    }).catch(err => { 
      console.error("Failed to obtain the image pixel map information." + JSON.stringify(err)); 
      return; 
    }) 
    let notificationRequest = { 
      id: 1, 
      content: { 
        contentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE, 
        picture: { 
          title: 'test_title', 
          text: 'test_text', 
          additionalText: 'test_additionalText', 
          picture: pixelmap, 
          briefText: 'test_briefText', 
          expandedTitle: 'test_expandedTitle', 
        } 
      }, 
    } 
    // 发送通知 
    NotificationManager.publish(notificationRequest, (err) => { 
      if (err) { 
        console.error(`[ANS] failed to publish, error[${err}]`); 
        return; 
      } 
      console.info(`[ANS] publish success `); 
    }); 
  }).catch(err => { 
    console.error('create pixelmap failed ==========' + JSON.stringify(err)); 
    return; 
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

运行效果如下图所示。

分享
微博
QQ
微信
回复
2024-06-13 21:05:52
相关问题
应用在后台时发送请求失败问题
1413浏览 • 1回复 待解决
HarmonyOS 如何保证应用在后台不被挂起
1144浏览 • 1回复 待解决
HarmonyOS 应用在投屏期间如何保活
894浏览 • 1回复 待解决
应用在CPU的占用情况如何线上分析
2308浏览 • 1回复 待解决
HarmonyOS上消息通知类型哪些
677浏览 • 1回复 待解决