HarmonyOS开发(十):通知

news/2024/7/21 10:41:21 标签: harmonyos, 华为

1、通知概述

1.1、简介

应用可以通过通知接口发送通知消息,终端用户可以通过通知栏查看通知内容,也可以点击通知来打开应用。

通知使用的的常见场景:

  • 显示接收到的短消息、即使消息...
  • 显示应用推送消息
  • 显示当前正在进行的事件,如下载等

HarmonyOS通过ANS(Advanced Notification Service,通知系统服务)对通知消息进行管理,支持多种通知类型。

1.2、通知的业务流程

业务流程中由通知子系统、通知发送端、通知订阅组件

一条通知从通知发送端产生,发送到通知子系统,然后再由通知子系统分发给通知订阅端。

1.3、通知消息的表现形式

 1.4、通知结构

1、通知小图图标:表示通知的功能与类型

2、通知名称:应用名称或者功能名称

3、时间:发送通知的时间,系统默认显示

4、展示图标:用来展开被折叠的内容和按钮,如果没有折叠的内容和按钮,则不显示这个图标

5、内容标题

6、内容详情

2、基础类型通知

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

普通文本类型                NOTIFICATION_CONTENT_BASIC_TEXT

长文本类型                    NOTIFICATION_CONTENT_LONG_TEXT

多行文本类型                NOTIFICATION_CONTENT_MULTILINE

图片类型                       NOTIFICATION_CONTENT_PICTURE

2.1、相关接口

接口名描述
publish(request:NotificatioinRequest,callback:AsyncCallback<void>):void发布通知
cancel(id:number,label:string,callback:AsyncCallback<void>):void取消指定的通知
cancelAll(callback:AsycCallback<void>):void取消所有该应用发布的通知

2.2、开发步骤

1、导入模块

import NotificationManager from '@ohos.notificationManager';

2、构造NotificationRequest对象,发布通知

import notificationManager from '@ohos.notificationManager'
import Prompt from '@system.prompt'
import image from '@ohos.multimedia.image';

@Entry
@Component
struct Index {

  publishNotification() {
    let notificationRequest: notificationManager.NotificationRequest = {
      id: 0,
      slotType: notificationManager.SlotType.SERVICE_INFORMATION,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal:{
          title: '通知标题',
          text: '这是一个通知的消息内容',
          additionalText: '通知附加内容'  // 附加内容是对通知内容的补充
        }
      }
    };
    notificationManager.publish(notificationRequest).then(() => {
      // 发布通知
      Prompt.showToast({
        message: '发布通知消息成功',
        duration: 2000
      })
    }).catch((err) => {
      Prompt.showToast({
        message: `发布通知失败,失败代码:${err.code},失败原因:${err.message}`,
        duration: 2000
      })
    });
  }

  /* 多行文本通知 */
  publicNotification1(){
    let notificationRequest:notificationManager.NotificationRequest = {
      id: 1,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,
        multiLine: {
          title: '通知标题',
          text: '通知的内容简介',
          briefText: 'briefText内容',
          longTitle: 'longTitle内容',
          lines: ['第一行内容','第二行内容','第三行内容']
        }
      }
    };
    notificationManager.publish(notificationRequest).then(() => {
      // 发布通知
      Prompt.showToast({
        message: '发布通知消息成功',
        duration: 2000
      })
    }).catch((err) => {
      Prompt.showToast({
        message: `发布通知失败,失败代码:${err.code},失败原因:${err.message}`,
        duration: 2000
      })
    });
  }

  /* 图片通知 */
  async publishPictureNotification(){
    // 把资源图片转为PixelMap对象
    let resourceManager = getContext(this).resourceManager;
    let imageArray = await resourceManager.getMediaContent($r('app.media.image2').id);
    let imageResource = image.createImageSource(imageArray.buffer);
    let pixelMap = await imageResource.createPixelMap();

    // 描述通知信息
    let notificationRequest:notificationManager.NotificationRequest = {
      id: 2,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
        picture: {
          title: '通知消息的标题',
          text: '展开查看详情,通知内容',
          expandedTitle: '展开时的内容标题',
          briefText: '这里是通知的概要内容,对通知的总结',
          picture: pixelMap
        }
      }
    };

    notificationManager.publish(notificationRequest).then(() => {
      // 发布通知消息
      Prompt.showToast({
        message: '发布通知消息成功',
        duration: 2000
      })
    }).catch((err) => {
      Prompt.showToast({
        message: `发布通知失败,失败代码:${err.code},失败原因:${err.message}`,
        duration: 2000
      })
    });
  }

  build() {
    Row() {
      Column() {
        Button('发送通知')
          .width('50%')
          .margin({bottom:10})
          .onClick(() => {
            this.publishNotification()
          })
        Button('发送多行文本通知')
          .width('50%')
          .margin({bottom:10})
          .onClick(() => {
            this.publicNotification1()
          })
        Button('发送图片通知')
          .width('50%')
          .margin({bottom:10})
          .onClick(() => {
            this.publishPictureNotification()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

3、进度条类型通知

进度条通知也是常见的通知类型,主要应用于文件下载、事务处理进度的显示。

HarmonyOS提供了进度条模板,发布通知应用设置好进度条模板的属性值,通过通知子系统发送到通知栏显示。

当前系统模板仅支持进度条模板,通知模板NotificationTemplate中的data参数为用户自定义数据,用来显示模块相关的数据。

3.1、相关接口

isSupportTemplate(templateName: string,callback:AsyncCallback<boolean>) : void        查询模板是否存在

3.2、开发步骤

1、导入模块

import NotificationManager from '@ohos.notificationManager'

2、查询系统是否支持进度条模板

NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {
    let isSupportTpl: boolean = data;    // 这里为true则表示支持模板
    // ...
}).catch((err) => {
    console.error('查询失败')
})

3、在第2步之后,再构造进度条模板对象,发布通知

import notificationManager from '@ohos.notificationManager'
import Prompt from '@system.prompt'

@Entry
@Component
struct ProgressNotice {

  async publishProgressNotification() {
    let isSupportTpl: boolean;
    await notificationManager.isSupportTemplate('downloadTemplate').then((data) => {
      isSupportTpl = data;
    }).catch((err) => {
      Prompt.showToast({
        message: `判断是否支持进度条模板时报错,error[${err}]`,
        duration: 2000
      })
    })
    if(isSupportTpl) {
      // 构造模板
      let template = {
        name: 'downloadTemplate',
        data: {
          progressValue: 60, // 当前进度值
          progressMaxValue: 100 // 最大进度值
        }
      };

      let notificationRequest: notificationManager.NotificationRequest = {
        // id: 100, // 这里的id可以不传
        content : {
          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
          normal: {
            title: '文件下载:鸿蒙学习手册.pdf',
            text: '下载进度',
            additionalText: '60%'
          }
        },
        template: template
      };

      // 发布通知
      notificationManager.publish(notificationRequest).then(() => {
        Prompt.showToast({
          message: `发布通知成功!`,
          duration: 2000
        })
      }).catch((err) => {
        Prompt.showToast({
          message: `发布通知失败,error[${err}]`,
          duration: 2000
        })
      })

    } else {
      Prompt.showToast({
        message: '不支持downloadTemplate进度条通知模板',
        duration: 2000
      })
    }

  }

  build() {
    Row() {
      Column() {
        Button('发送进度条通知')
          .width('50%')
          .onClick(()=>{
            this.publishProgressNotification()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

4、为通知添加行为意图

WantAgent提供了封装行为意图的能力,这里的行为意图能力就要是指拉起指定的应用组件及发布公共事件等能力。

HarmonyOS支持以通知的形式,把WantAgent从发布方传递到接收方,从而在接收方触发WantAgent中指定的意图。

为通知添加行为意图的实现方式如上图所示,发布通知的应用向组件管理服务AMS(Ability Manager Service)申请WantAgent,然后随其他通知信息 一起发送给桌面,当用户在桌面通知栏上点击通知时,触发WantAgent动作。

4.1、相关接口

接口名描述
getWantAgent(info: WantAgentInfo, callback:AsyncCallback<WantAgent>):void创建WantAgent
trigger(agent:WantAgent, triggerInfo:TrggerInfo,callback?:Callback<CompleteData>):void触发WantAgent意图
cancel(agent:WantAgent,callback:AsyncCallback<void>):void取消WantAgent
getWant(agent:WantAgent,callback:AsyncCallback<Want>):void获取WantAgent的want
equal(agent:WantAgent,otherAgent:WantAgent,callback:AsyncCallback<boolean>):void判断两个WantAgent实例是否相等

4.2、开发步骤

1、导入模块

import NotificationManager from '@ohos.notificationManager';
import wantAgent from '@ohos.app.ability.wantAgent';

 


http://www.niftyadmin.cn/n/5243637.html

相关文章

【第二章】用于基因组数据分析的 R 简介

计算基因组学的目的是从高维基因组数据中提供生物学解释和见解。一般来说&#xff0c;它与任何其他类型的数据分析工作类似&#xff0c;但通常进行计算基因组学需要特定领域的知识和工具。 计算基因组学的目的是从高维基因组数据中提供生物学解释和见解。一般来说&#xff0c;它…

Python替代Adobe从PDF提取数据

大家好&#xff0c;PDF文件是官方报告、发票和数据表的通用格式&#xff0c;然而从PDF文件中提取表格数据是一项挑战。尽管Adobe Acrobat等工具提供了解决方案&#xff0c;但它们并不总是易于获取或可自动化运行&#xff0c;而Python则是编程语言中的瑞士军刀。本文将探讨如何利…

SpringCloud常见问题

1、什么是Spring Cloud&#xff1f; Spring Cloud是一款基于Spring Boot框架开发的微服务框架&#xff0c;它为开发人员提供了一系列的组件和工具&#xff0c;可以帮助开发人员快速构建和部署微服务&#xff0c;提高开发效率和项目可维护性。Spring Cloud提供了包括服务注册与…

Domino多Web站点托管

大家好&#xff0c;才是真的好。 看到一篇文档&#xff0c;大概讲述的是他在家里架了一台Domino服务器&#xff0c;上面跑了好几个Internet的Web网站&#xff08;使用Internet站点&#xff09;。再租了一台云服务器&#xff0c;上面安装Nginx做了反向代理&#xff0c;代理访问…

配置和管理VLAN

VLAN技术是交换技术的重要组成部分&#xff0c;也是交换机配置的基础。用于把物理上直接相连的网络从逻辑上划分为多个子网。 每一个VLAN 对应一个广播域&#xff0c;处于不同VLAN 上的主机不能通信。 不同VLAN 之间通信需要引入三层交换技术。 对性能局域网的配置和管理主要…

探索人工智能领域——每日20个名词详解【day10】

目录 前言 正文 总结 &#x1f308;嗨&#xff01;我是Filotimo__&#x1f308;。很高兴与大家相识&#xff0c;希望我的博客能对你有所帮助。 &#x1f4a1;本文由Filotimo__✍️原创&#xff0c;首发于CSDN&#x1f4da;。 &#x1f4e3;如需转载&#xff0c;请事先与我联系以…

【FPGA图像处理实战】- FPGA图像处理仿真测试工程(读写BMP图片)

FPGA开发过程中“行为功能仿真”是非常必要的一个过程&#xff0c;如果仿真都没通过&#xff0c;则上板测试必定失败。 FPGA图像处理需要读写大量的图像数据&#xff0c;单看这些图像数据实际是没有规则的&#xff0c;如果直接上板测试&#xff0c;调试起来非常困难&#xff0…

kyuubi整合flink yarn session mode

目录 概述配置flink 配置kyuubi 配置kyuubi-defaults.confkyuubi-env.shhive 验证启动kyuubibeeline 连接使用hive catlogsql测试 结束 概述 flink 版本 1.17.1、kyuubi 1.8.0、hive 3.1.3、paimon 0.5 整合过程中&#xff0c;需要注意对应的版本。 注意以上版本 配置 ky…