51.HarmonyOS鸿蒙系统 App(ArkUI)通知

2024-04-25 01:28

本文主要是介绍51.HarmonyOS鸿蒙系统 App(ArkUI)通知,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

普通文本通知测试

长文本通知测试

多行文本通知测试

图片通知测试

进度条通知测试

通知简介

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

通知常见的使用场景:

  • 显示接收到的短消息、即时消息等。

  • 显示应用的推送消息,如广告、版本更新等。

  • 显示当前正在进行的事件,如下载等。

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

通知业务流程

通知业务流程由通知子系统、通知发送端、通知订阅端组成。一条通知从通知发送端产生,通过IPC通信发送到通知子系统,再由通知子系统分发给通知订阅端。

  • 通知发送端:可以是三方应用或系统应用。开发者重点关注。

  • 通知订阅端:只能为系统应用,比如通知中心。通知中心默认会订阅手机上所有应用对当前用户的通知。开发者无需关注。

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

表1 基础类型通知中的内容分类

类型

描述

NOTIFICATION_CONTENT_BASIC_TEXT

普通文本类型。

NOTIFICATION_CONTENT_LONG_TEXT

长文本类型。

NOTIFICATION_CONTENT_MULTILINE

多行文本类型。

NOTIFICATION_CONTENT_PICTURE

图片类型。

 

import NotificationManager from '@ohos.notificationManager';import image from '@ohos.multimedia.image';
import wantAgent from '@ohos.app.ability.wantAgent';
//import { Header} from '../common/components/CommonComponents'
//为通知添加行为意图,点击通知栏启动相关app应用程序,呼出对应应用程序let wantAgentObj = null; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
// 通过WantAgentInfo的operationType设置动作类型。
let wantAgentInfo = {wants: [{deviceId: '',bundleName: 'com.example.myapplication',abilityName: 'EntryAbility',action: '',entities: [],uri: '',parameters: {}}],operationType: wantAgent.OperationType.START_ABILITY,requestCode: 0,wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
}
// 创建WantAgent
// wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
//   if (err) {
//     console.error('[WantAgent]getWantAgent err=' + JSON.stringify(err));
//   } else {
//     console.info('[WantAgent]getWantAgent success');
//     wantAgentObj = data;
//   }
// });// 构造NotificationRequest对象行为意图
let notificationRequest_behavior = {content: {contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: '行为意图Test_Title_呼出app',text: '行为意图Test_Text',additionalText: 'Test_AdditionalText',},},id: 6,label: 'TEST',wantAgent: wantAgentObj,
}// 图片构造
const color = new ArrayBuffer(60000);
let bufferArr = new Uint8Array(color);
for (var i = 0; i<bufferArr.byteLength;i++) {bufferArr[i++] = 60;bufferArr[i++] = 20;bufferArr[i++] = 220;bufferArr[i] = 100;
}
let opts = { editable:true, pixelFormat:"ARGB_8888", size: {height:100, width : 150}};
let imagePixelMap: PixelMap = undefined;NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {console.info(`[ANS] isSupportTemplate success`);console.info('Succeeded in supporting download template notification.');let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持
}).catch((err) => {console.error(`Failed to support download template notification. Code is ${err.code}, message is ${err.message}`);
});
let template = {name:'downloadTemplate',data: {title: '标题:',fileName: 'music.mp4',progressValue: 30,progressMaxValue:100,}
}
//构造NotificationRequest对象
let notificationRquest_progress = {id: 5,slotType: NotificationManager.SlotType.OTHER_TYPES,template: template,content: {contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: template.data.title + template.data.fileName,text: "sendTemplate",additionalText: "30%"}},deliveryTime: new Date().getTime(),showDeliveryTime: true
}let notificationRequest2 = {id:1,content:{contentType:NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,//普通文本类型通知normal:{title:'测试标题123',text:'测试内容567',additionalText:'普通文本类型通知'}}
}
let notificationRequest_long = {id:2,content:{contentType:NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,//长文本类型通知longText:{title:'长文本类型通知标题123',text:'测试内容567',additionalText:'长文本类型通知',longText: 'test_longText',briefText: 'test_briefText',expandedTitle: '长文本类型通知标题',}}
}
let notificationRequest_multiline = {id:3,content:{contentType:NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,//多行文本类型通知multiLine:{title:'多行文本类型标题123',text:'多行文本类型内容567',briefText: 'test_briefText',longTitle: '多行文本类型',lines: ['1行', '2行', '3行', '4行','5行'],}}
}
//
//Argument of type '{ id: number; content: { contentType: NotificationManager.ContentType; normal: { title: string; test: string; additionalText: string; }; }; }' is not assignable to parameter of type 'NotificationRequest'. The types of 'content.normal' are incompatible between these types. Property 'text' is missing in type '{ title: string; test: string; additionalText: string; }' but required in type 'NotificationBasicContent'. <tsCheck>
//Argument of type '{ id: number; content: { contentType: NotificationManager.ContentType; normal: { title: string; test: string; additionalText: string; }; }; }' is not assignable to parameter of type 'NotificationRequest'. <tsCheck>@Entry
@Component
struct Index {@State message: string = 'Hello World你好,世界很精彩'@State imagePixelMap: PixelMap = undefined;@State url :string =''// 全局任务idindex: number = 6@State bundleName: string = ''public async get_bundleName() {this.bundleName = await globalThis.context.abilityInfo.bundleName}//aboutToAppear函数在创建自定义组件的新实例后,在执行其build()函数之前执行。允许在aboutToAppear函数中改变状态变量,更改将在后续执行build()函数中生效。async aboutToAppear() {{let rm = getContext(this).resourceManager;// 读取图片let file = await rm.getMediaContent($r('app.media.leaf'))// 创建PixelMapimage.createImageSource(file.buffer).createPixelMap().then(value => this.imagePixelMap = value).catch(reason => console.log('testTag', '加载图片异常', JSON.stringify(reason)))}}// 图片型文本发送publishPictureNotification() {let request: NotificationManager.NotificationRequest = {id: this.index++,content: {contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,picture: {title: '图片通知标题' + this.index,text: '图片通知内容详情',additionalText: '图片通知附加内容',briefText: '图片通知概要和总结',expandedTitle: '图片展开后标题' + this.index,picture: this.imagePixelMap}}}NotificationManager.publish(request,(err)=>{if(err){console.error(`图片类型通知失败,err[${err}]`);return;}console.info('图片文本类型通知成功')});}httpRequest2(){let aa:PixelMap = undefinedlet imageRes = image.createImageSource(this.url)}build() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)Button('普通文本通知测试').onClick(()=>{NotificationManager.publish(notificationRequest2,(err)=>{if(err){console.error(`普通文本类型通知失败,err[${err}]`);return;}console.info('普通文本类型通知成功')});})Button('长文本通知测试').onClick(()=>{NotificationManager.publish(notificationRequest_long,(err)=>{if(err){console.error(`长文本通知测试失败,err[${err}]`);return;}console.info('长文本通知测试成功')});})Button('多行文本通知测试').onClick(()=>{NotificationManager.publish(notificationRequest_multiline,(err)=>{if(err){console.error(`多行文本通知测试失败,err[${err}]`);return;}console.info('多行文本通知测试成功')});})Button('图片通知测试').onClick(()=>{// this.url = $r('app.media.icon')this.publishPictureNotification();})Button('进度条通知测试').onClick(()=>{NotificationManager.publish(notificationRquest_progress,(err)=>{if(err){console.error(`进度条通知测试失败,err[${err}]`);return;}console.info('进度条通知测试成功')});})// Button('行为意图通知测试_呼出app')//   .onClick(()=>{//////   })}.width('100%')}.height('100%')}
}

 

 

这篇关于51.HarmonyOS鸿蒙系统 App(ArkUI)通知的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/933362

相关文章

linux系统中java的cacerts的优先级详解

《linux系统中java的cacerts的优先级详解》文章讲解了Java信任库(cacerts)的优先级与管理方式,指出JDK自带的cacerts默认优先级更高,系统级cacerts需手动同步或显式... 目录Java 默认使用哪个?如何检查当前使用的信任库?简要了解Java的信任库总结了解 Java 信

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

Oracle数据库在windows系统上重启步骤

《Oracle数据库在windows系统上重启步骤》有时候在服务中重启了oracle之后,数据库并不能正常访问,下面:本文主要介绍Oracle数据库在windows系统上重启的相关资料,文中通过代... oracle数据库在Windows上重启的方法我这里是使用oracle自带的sqlplus工具实现的方

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux查询服务器系统版本号的多种方法

《Linux查询服务器系统版本号的多种方法》在Linux系统管理和维护工作中,了解当前操作系统的版本信息是最基础也是最重要的操作之一,系统版本不仅关系到软件兼容性、安全更新策略,还直接影响到故障排查和... 目录一、引言:系统版本查询的重要性二、基础命令解析:cat /etc/Centos-release详

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

在Linux系统上连接GitHub的方法步骤(适用2025年)

《在Linux系统上连接GitHub的方法步骤(适用2025年)》在2025年,使用Linux系统连接GitHub的推荐方式是通过SSH(SecureShell)协议进行身份验证,这种方式不仅安全,还... 目录步骤一:检查并安装 Git步骤二:生成 SSH 密钥步骤三:将 SSH 公钥添加到 github

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方