【中秋国庆不断更】HarmonyOS对通知类消息的管理与发布通知(上) ...

打印 上一主题 下一主题

主题 932|帖子 932|积分 2796

【中秋国庆不断更】HarmonyOS对通知类消息的管理与发布通知(上)

一、 通知概述

通知简介

应用可以通过通知接口发送通知消息,终端用户可以通过通知栏查看通知内容,也可以点击通知来打开应用。
通知常见的使用场景:
​                ● 显示接收到的短消息、即时消息等。
​                ● 显示应用的推送消息,如广告、版本更新等。
​                ● 显示当前正在进行的事件,如下载等。
HarmonyOS通过ANS(Advanced Notification Service,通知系统服务)对通知类型的消息进行管理,支持多种通知类型,如基础类型通知、进度条类型通知。
通知业务流程

通知业务流程由通知子系统、通知发送端、通知订阅端组成。
一条通知从通知发送端产生,通过IPC通信发送到通知子系统,再由通知子系统分发给通知订阅端。
系统应用还支持通知相关配置,如使能开关、配置参数由系统配置发起请求,发送到通知子系统存储到内存和数据库。

二、发布基础类型通知

基础类型通知主要应用于发送短信息、提示信息、广告推送等,支持普通文本类型、长文本类型、多行文本类型和图片类型。
表1 基础类型通知中的内容分类
类型描述NOTIFICATION_CONTENT_BASIC_TEXT普通文本类型。NOTIFICATION_CONTENT_LONG_TEXT长文本类型。NOTIFICATION_CONTENT_MULTILINE多行文本类型。NOTIFICATION_CONTENT_PICTURE图片类型。目前系统仅通知栏订阅了通知,将通知显示在通知栏里。基础类型通知呈现效果示意图如下所示。
图1 基础类型通知呈现效果示意图

接口说明

通知发布接口如下表所示,不同发布类型通知由NotificationRequest的字段携带不同的信息。
接口名描述publish(request: NotificationRequest, callback: AsyncCallback): void发布通知。cancel(id: number, label: string, callback: AsyncCallback): void取消指定的通知。cancelAll(callback: AsyncCallback): 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. // 图片构造
  2. const color = new ArrayBuffer(60000);
  3. let bufferArr = new Uint8Array(color);
  4. for (var i = 0; i<bufferArr.byteLength;i++) {
  5.   bufferArr[i++] = 60;
  6.   bufferArr[i++] = 20;
  7.   bufferArr[i++] = 220;
  8.   bufferArr[i] = 100;
  9. }
  10. let opts = { editable:true, pixelFormat:"ARGB_8888", size: {height:100, width : 150}};
  11. await image
  12.   .createPixelMap(color, opts)
  13.   .then(async (pixelmap) => {
  14.     await pixelmap.getImageInfo().then(imageInfo => {
  15.       console.log("=====size: ====" + JSON.stringify(imageInfo.size));
  16.     }).catch(err => {
  17.       console.error("Failed to obtain the image pixel map information." + JSON.stringify(err));
  18.       return;
  19.     })
  20.     let notificationRequest = {
  21.       id: 1,
  22.       content: {
  23.         contentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,
  24.         picture: {
  25.           title: 'test_title',
  26.           text: 'test_text',
  27.           additionalText: 'test_additionalText',
  28.           picture: pixelmap,
  29.           briefText: 'test_briefText',
  30.           expandedTitle: 'test_expandedTitle',
  31.         }
  32.       },
  33.     }
  34.     // 发送通知
  35.     NotificationManager.publish(notificationRequest, (err) => {
  36.       if (err) {
  37.         console.error(`[ANS] failed to publish, error[${err}]`);
  38.         return;
  39.       }
  40.       console.info(`[ANS] publish success `);
  41.     });
  42.   }).catch(err=>{
  43.     console.error('create pixelmap failed =========='+ JSON.stringify(err));
  44.     return;
  45.   })
复制代码
运行效果如下图所示。

本文由博客一文多发平台 OpenWrite 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表