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

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

在这里插入代码片
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]
          }
          // 创建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%')
  }
}
  • 17
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹荣乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值