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

文章目录

一、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、普通文本类型。

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

javascript 复制代码
在这里插入代码片
javascript 复制代码
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、长文本类型。

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

javascript 复制代码
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、多行文本类型。

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

javascript 复制代码
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的意图,从而拉起目标应用组件。

javascript 复制代码
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]
          }
          // 创建WantAgent
          let 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%')
  }
}
相关推荐
亚历克斯神5 小时前
Flutter for OpenHarmony: Flutter 三方库 mutex 为鸿蒙异步任务提供可靠的临界资源互斥锁(并发安全基石)
android·数据库·安全·flutter·华为·harmonyos
钛态5 小时前
Flutter 三方库 smartstruct 鸿蒙化字段映射适配指南:介入静态预编译引擎扫除视图及数据模型双向强转类型错乱隐患,筑稳如磐石的企业级模型治理防线-适配鸿蒙 HarmonyOS ohos
flutter·华为·harmonyos
键盘鼓手苏苏5 小时前
Flutter 组件 csv2json 适配鸿蒙 HarmonyOS 实战:高性能异构数据转换,构建 CSV 流式解析与全栈式数据映射架构
flutter·harmonyos·鸿蒙·openharmony
雷帝木木5 小时前
Flutter 三方库 hrk_logging 的鸿蒙化适配指南 - 实现标准化分层日志记录、支持多目的地输出与日志分级过滤
flutter·harmonyos·鸿蒙·openharmony·hrk_logging
左手厨刀右手茼蒿5 小时前
Flutter 三方库 dio_compatibility_layer 的鸿蒙化适配指南 - 实现 Dio 跨主版本的平滑迁移、支持遗留拦截器兼容与网络请求架构稳定升级
flutter·harmonyos·鸿蒙·openharmony·dio_compatibility_layer
雷帝木木5 小时前
Flutter 三方库 hashids2 基于鸿蒙安全内核的深度隐匿映射适配:数字指纹泄露防御层、生成短小精悍唯一不可逆加盐哈希,护航全链路请求 URL 隐私-适配鸿蒙 HarmonyOS ohos
安全·flutter·harmonyos
HwJack207 小时前
HarmonyOS响应式布局与窗口监听:让界面像呼吸般灵动的艺术
ubuntu·华为·harmonyos
王码码20358 小时前
Flutter 组件 inappwebview_cookie_manager 适配 鸿蒙Harmony 实战 - 驾驭核心大 Web 容器缓存隧道、构建金融级政企应用绝对防串号跨域大隔离基座
flutter·harmonyos·鸿蒙·openharmony·inappwebview_cookie_manager
左手厨刀右手茼蒿8 小时前
Flutter 组件 ews 的适配 鸿蒙Harmony 实战 - 驾驭企业级 Exchange Web Services 协议、实现鸿蒙端政企办公同步与高安通讯隔离方案
flutter·harmonyos·鸿蒙·openharmony
键盘鼓手苏苏8 小时前
Flutter 组件 spry 适配鸿蒙 HarmonyOS 实战:轻量化 Web 框架,构建高性能端侧微服务与 Middleware 治理架构
flutter·harmonyos·鸿蒙·openharmony