介绍
本篇Codelab主要介绍如何使用通知能力和基础组件,实现模拟下载文件,发送通知的案例。
相关概念
- 通知:应用可以通过通知接口发送通知消息,终端用户可以通过通知栏查看通知内容,也可以点击通知来打开应用。
完整示例
源码下载
环境搭建
我们首先需要完成HarmonyOS开发环境搭建,可参照如下步骤进行。
软件要求
- DevEco Studio版本:DevEco Studio 3.1 Release。
- HarmonyOS SDK版本:API version 9。
硬件要求
- 设备类型:华为手机或运行在DevEco Studio上的华为手机设备模拟器。
- HarmonyOS系统:3.1.0 Developer Release。
环境搭建
-
安装DevEco Studio,详情请参考下载和安装软件。
- 设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:
- 如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。
- 如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。
- 开发者可以参考以下链接,完成设备调试的相关配置:
代码结构解读
本篇Codelab只对核心代码进行讲解,对于完整代码,我们会在源码下载或gitee中提供。
├──entry/src/main/ets // 代码区
│ ├──common
│ │ ├──constants
│ │ │ └──CommonConstants.ets // 公共常量类
│ │ └──utils
│ │ ├──Logger.ets // 日志工具类
│ │ ├──NotificationUtil.ets // 通知工具类
│ │ └──ResourseUtil.ets // 资源文件工具类
│ ├──entryability
│ │ └──EntryAbility.ts // 程序入口类
│ └──pages
│ └──MainPage.ets // 主页面
└──entry/src/main/resources // 资源文件目录
发送通知
本章节将以进度条类型的通知为例,介绍如何发送通知。需要完成以下步骤:
- 导入通知模块,查询系统是否支持进度条模板。
- 获取点击通知拉起应用时,需要的Want信息。
- 构造进度条模板对象,并发布通知。
// NotificationUtil.ets
import notification from '@ohos.notificationManager';
...
// 获取应用的Want信息
export function createWantAgent(bundleName: string, abilityName: string): Promise<WantAgent> {
let wantAgentInfo = {
wants: [
{
bundleName: bundleName,
abilityName: abilityName
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
} as wantAgent.WantAgentInfo;
return wantAgent.getWantAgent(wantAgentInfo);
}
// 发布通知
export function publishNotification(progress: number, title: string, wantAgentObj: WantAgent) {
// 构造通知模板对象
let template: notification.NotificationTemplate = {
name: 'downloadTemplate',
data: {
progressValue: progress,
progressMaxValue: CommonConstants.PROGRESS_TOTAL,
isProgressIndeterminate: false
}
};
// 构造NotificationRequest对象
let notificationRequest: notification.NotificationRequest = {
id: CommonConstants.NOTIFICATION_ID,
slotType: notification.SlotType.CONTENT_INFORMATION,
// 模板对象
template: template,
content: {
contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: `${title}:${CommonConstants.DOWNLOAD_FILE}`,
text: '',
additionalText: `${progress}%`
}
},
// 点击拉起应用的Want信息
wantAgent: wantAgentObj
};
// 发送通知
notification.publish(notificationRequest).catch((err: Error) => {
Logger.error(`[ANS] publish failed,message is ${err}`);
});
}
模拟下载
本章节介绍模拟文件下载的过程。文件下载共有四种状态,分别为初始化、下载中、暂停下载、下载完成。主要实现以下功能:
- 初始化状态,点击下载,启动Interval定时器,持续发送通知。
- 下载中,点击暂停,清除定时器,发送一次通知,显示当前进度。
- 暂停下载,点击继续,重新启动定时器,重复步骤一。
- 下载完成,清除定时器。
// MainPage.ets
@Entry
@Component
struct MainPage {
@State downloadStatus: number = DOWNLOAD_STATUS.INITIAL;
@State downloadProgress: number = 0;
...
// 定时器,发送通知
download() {
this.interval = setInterval(async () => {
if (this.downloadProgress === CommonConstants.PROGRESS_TOTAL) {
this.notificationTitle = await getStringByRes($r('app.string.notification_title_finish'), this);
this.downloadStatus = DOWNLOAD_STATUS.FINISHED;
clearInterval(this.interval);
} else {
this.downloadProgress += CommonConstants.PROGRESS_SPEED;
}
if (this.isSupport) {
publishNotification(this.downloadProgress, this.notificationTitle, this.wantAgentObj);
}
}, CommonConstants.UPDATE_FREQUENCY);
}
// 点击下载
async start() {
this.notificationTitle = await getStringByRes($r('app.string.notification_title_download'), this);
this.downloadStatus = DOWNLOAD_STATUS.DOWNLOADING;
this.downloadProgress = 0;
this.download();
}
// 点击暂停
async pause() {
this.notificationTitle = await getStringByRes($r('app.string.notification_title_pause'), this);
clearInterval(this.interval);
this.downloadStatus = DOWNLOAD_STATUS.PAUSE;
if (this.isSupport) {
publishNotification(this.downloadProgress, this.notificationTitle, this.wantAgentObj);
}
}
// 点击继续
async resume() {
this.notificationTitle = await getStringByRes($r('app.string.notification_title_download'), this);
this.download();
this.downloadStatus = DOWNLOAD_STATUS.DOWNLOADING;
}
// 点击取消
async cancel() {
this.downloadProgress = 0;
clearInterval(this.interval);
this.downloadStatus = DOWNLOAD_STATUS.INITIAL;
notification.cancel(CommonConstants.NOTIFICATION_ID);
}
}
总结
您已经完成了本次Codelab的学习,并了解到以下知识点:
- 使用进度条类型的通知,完成模拟下载文件。