当前位置: 首页 > news >正文

鸿蒙HarmonyOS开发:用户通知服务Noification的详细使用指南

文章目录

      • 一、Notification Kit简介
      • 二、能力范围
      • 三、业务流程
      • 四、通知样式:
      • 五、约束限制
      • 六、开发步骤
        • 6.1、导入模块。
        • 6.2、构造NotificationRequest对象,并发布通知。
          • 6.2.1、普通文本类型。
          • 6.2.2、长文本类型。
          • 6.2.3、多行文本类型。
        • 6.3、为通知添加行为意图

一、Notification Kit简介

Notification Kit(用户通知服务)为开发者提供本地通知发布通道,开发者可借助Notification Kit将应用产生的通知直接在客户端本地推送给用户,本地通知根据通知类型及发布场景会产生对应的铃声、震动、横幅、锁屏、息屏、通知栏提醒和显示。

在这里插入图片描述

二、能力范围

Notification Kit支持的能力主要包括:

  • 发布文本、多行文本、通知大图标等类型通知。
  • 携带或更新应用通知数字角标。
  • 取消曾经发布的某条或全部通知。
  • 查询已发布的通知列表。
  • 查询应用自身通知开关状态。
  • 应用通知用户的能力默认关闭,开发者可拉起授权框,请求用户授权发布通知。

三、业务流程

使用Noification Kit的主要业务流程如下:

  1. 请求通知授权。
  2. 应用发布通知到通知服务。
  3. 将通知展示到通知中心。

四、通知样式:

在这里插入图片描述

五、约束限制

  • 单个应用已发布的通知在通知中心等系统入口的留存数量有限(当前规格最多24条)。
  • 通知的长度不能超过200KB(跨进程序列化大小限制)。
  • 系统所有应用发布新通知的频次累计不能超过每秒10条,更新通知的频次累计不能超过每秒20条。

六、开发步骤

6.1、导入模块。
import notificationManager from '@ohos.notificationManager';
6.2、构造NotificationRequest对象,并发布通知。
6.2.1、普通文本类型。

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

在这里插入代码片
import notificationManager from '@ohos.notificationManager';@Entry
@Component
struct Notification {build() {Column() {Button("普通文本").onClick(() => {let notificationRequest = {id: 1,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知normal: {title: 'test_title',text: 'test_text',additionalText: 'test_additionalText',},},deliveryTime: new Date().getTime(),showDeliveryTime: true}notificationManager.publish(notificationRequest, (err) => {if (err) {console.error(`[ANS] failed to publish, error[${err}]`);return;}console.info(`[ANS] publish success`);});})}.width('100%').height('100%')}
}
6.2.2、长文本类型。

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

import notificationManager from '@ohos.notificationManager';@Entry
@Component
struct Notification {build() {Column() {Button("长文本").onClick(() => {let notificationRequest = {id: 2,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知longText: {title: 'test_title',text: 'test_text',additionalText: 'test_additionalText',longText: 'test_longText test_longText test_longText test_longText test_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`);});})}.width('100%').height('100%')}
}
6.2.3、多行文本类型。

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

import notificationManager from '@ohos.notificationManager';@Entry
@Component
struct Notification {build() {Column() {Button("多行文本").onClick(() => {let notificationRequest = {id: 3,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本类型通知multiLine: {title: 'test_title',text: 'test_text',briefText: 'test_briefText',longTitle: 'test_longTitle',lines: ['line_01', 'line_02', 'line_03', 'line_04'],}}}notificationManager.publish(notificationRequest, (err) => {if (err) {console.error(`[ANS] failed to publish, error[${err}]`);return;}console.info(`[ANS] publish success`);});})}.width('100%').height('100%')}
}
6.3、为通知添加行为意图

WantAgent提供了封装行为意图的能力,这里所说的行为意图主要是指拉起指定的应用组件及发布公共事件等能力。HarmonyOS支持以通知的形式,将WantAgent从发布方传递至接收方,从而在接收方触发WantAgent中指定的意图。例如,在通知消息的发布者发布通知时,通常期望用户可以通过通知栏点击拉起目标应用组件。为了达成这一目标,开发者可以将WantAgent封装至通知消息中,当系统接收到WantAgent后,在用户点击通知栏时触发WantAgent的意图,从而拉起目标应用组件。

import notificationManager from '@ohos.notificationManager';
import wantAgent from '@ohos.app.ability.wantAgent';@Entry
@Component
struct Notification {build() {Column() {Button("为通知添加行为意图").onClick(async () => {// 通过WantAgentInfo的operationType设置动作类型。let wantAgentInfo = {wants: [{bundleName: 'com.example.myapplication',abilityName: 'EntryAbility',parameters: {}}],operationType: wantAgent.OperationType.START_ABILITY,requestCode: 0,wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]}// 创建WantAgentlet wantAgentObj = await wantAgent.getWantAgent(wantAgentInfo);let notificationRequest = {id: 4,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知normal: {title: 'test_title4',text: 'test_text4',additionalText: 'test_additionalText4',},},deliveryTime: new Date().getTime(),showDeliveryTime: true,wantAgent: wantAgentObj,}notificationManager.publish(notificationRequest, (err) => {if (err) {console.error(`[ANS] failed to publish, error[${err}]`);return;}console.info(`[ANS] publish success`);});})}.width('100%').height('100%')}
}

http://www.mrgr.cn/news/1359.html

相关文章:

  • sed awk 第二版学习(一)—— sed 与 awk 基本操作
  • 贝叶斯推理:分步指南
  • WPF-实现多语言的静态(需重启)与动态切换(不用重启)
  • 设计模式实战:数据分析系统的设计与实现
  • 2024新型数字政府综合解决方案(六)
  • C#中的多线程
  • 【数据结构】二叉树篇
  • 对react组件和组件化理解
  • Java项目集成RocketMQ
  • 《Docker:实现开发环境一致性与高效部署的利器》
  • 创建张量、张量的操作
  • Kafka运行机制(二):消息确认,消息日志的存储和回收
  • echarts渐变圆环进度条样式
  • jenkins pipline脚本 获取git分支
  • Snipaste 的一款替代工具 PixPin,支持 gif 截图、长截图和 OCR 文字识别,功能不是一点点强!
  • 虚幻5|角色武器装备的数据库学习(不只是用来装备武器,甚至是角色切换也很可能用到)
  • 《AI音频类工具之九——Stable Audio​ 》
  • TortoiseGit修改差异查看器为BeyondCompare
  • 网络丢包深度解析与优化:检测、诊断与减少策略
  • 《区块链与监管合规:在创新与规范之间寻求平衡》