news 2026/8/18 17:36:48

鸿蒙掌上驾考宝典应用开发49:鸿蒙推送服务——PushUtils 与通知管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
鸿蒙掌上驾考宝典应用开发49:鸿蒙推送服务——PushUtils 与通知管理

第49篇:鸿蒙推送服务——PushUtils 与通知管理

一、引言

推送服务是应用主动触达用户的重要渠道,用于发送考试提醒、学习通知、模考成绩等消息。DriverLicenseExam 项目通过鸿蒙推送服务实现了消息推送功能,包括通知权限申请、消息构建、推送发送、点击处理等完整流程。本文将深入解析推送服务的实现。

二、推送服务架构

2.1 推送流程

应用启动 │ ▼ 申请通知权限(requestEnableNotification) │ ▼ 权限已授权? ├── 否 → 不发送推送 │ └── 是 → 构建推送消息 │ ▼PushUtils.randomPushMessage()│ ▼ 系统通知栏显示 │ ▼ 用户点击通知 │ ▼EntryAbility.onNewWant()│ ▼ 解析参数 → 跳转到指定页面

2.2 推送相关文件

commons/commonLib/src/main/ets/push/├── Model.ets ← 推送数据模型 └── PushUtils.ets ← 推送工具类

三、推送权限管理

3.1 通知权限申请

在 EntryAbility 的onWindowStageCreate中申请通知权限:

//EntryAbility.ets notificationManager.requestEnableNotification(this.context).then(()=>{ hilog.info(0x0000,'testTag','[ANS] requestEnableNotification success'); }).catch((err: BusinessError)=>{ hilog.error(0x0000,'testTag','[ANS] requestEnableNotification failed, code: '+ err.code +', message: '+ err.message); });

3.2 权限检查

在发送推送前检查通知权限是否已开启:

// MainEntry.etssendPushNotice() {constisOn = notificationManager.isNotificationEnabledSync();if(isOn) {this.sendPushMessage(); } }

isNotificationEnabledSync()是同步方法,快速检查通知权限状态,避免在未授权时进行不必要的推送操作。

四、推送消息模型

4.1 推送参数定义

// PushUtils.etsexportinterfacePushActionParams{ picVideoUrl:string;// 图片/视频 URL(用于富媒体通知)title:string;// 推送标题id:string;// 推送 ID(用于去重和追踪)}

4.2 推送消息构建

// PushUtils.etsexportclassPushUtils{// 构建推送通知请求staticbuildNotificationRequest(params: PushActionParams): notificationManager.NotificationRequest{letnotificationRequest: notificationManager.NotificationRequest = { id: parseInt(params.id) ||0, content: { contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title:params.title, text:'点击查看详情', additionalText:params.picVideoUrl ||'', }, },// 点击通知后携带的参数wantAgent: {// 通过 Want 传递参数到 Abilitywant: { deviceId:'', bundleName:'qcjk.1.xxxxxx', abilityName:'EntryAbility', parameters: {params: JSON.stringify({ message:'practiceView', title:params.title, id:params.id, }), }, }, }, };returnnotificationRequest; } }

五、推送消息发送

5.1 随机推送消息

// PushUtils.etsstaticrandomPushMessage(params: PushActionParams, context: Context) {// 随机决定是否发送推送if(Math.random() >0.3) {// 30% 概率发送return; }constnotificationRequest =this.buildNotificationRequest(params); notificationManager.publish(notificationRequest) .then(() =>{Logger.info('PushUtils','Push notification published successfully'); }) .catch((err: BusinessError) =>{Logger.error('PushUtils','Failed to publish notification: '+ err.message); }); }

5.2 在主页面触发推送

// MainEntry.etsaboutToAppear(): void {this.bottomRectHeight = AppStorage.get('bottomRectHeight') ||0;this.vm.navStack.pushPathByName('splashPage',true);this.sendPushNotice();// 启动时尝试发送推送this.updateForm(0); } sendPushMessage() { let pushArticle: PushActionParams = { picVideoUrl:'', title:'驾考模板', id:'3445749589458989', }; PushUtils.randomPushMessage(pushArticle,this.getUIContext().getHostContext()asContext); }

六、推送点击处理

6.1 Want 参数解析

当用户点击通知栏的推送消息时,系统会通过 Want 参数将消息传递到 Ability:

// EntryAbility.ets - 处理推送点击onNewWant(want: Want,launchParam: AbilityConstant.LaunchParam): void { this.setLightOrDarkMode(this.context);Logger.info(TAG, 'Ability onNewWant');if(want.parameters&&want.parameters.params) {letparam: ESObject = {};try{ param =JSON.parse(want.parameters.paramsasstring); } catch (e) {Logger.error(TAG, 'Failedtoparse push params: ' +JSON.stringify(e)); }// 根据推送消息类型跳转到不同页面if(param.message==='practiceView') {// 跳转到模拟考试const examService =ExamService.instance(this.contextasContext); const par: ROUTE_PARAM = { title: '模拟考试',type:EXAM_MANAGER_TYPE.mock_exam, examManager: examService.getMockExamManager('模拟考试'), };CommonModel.instance.navStack.replacePathByName('practiceView',par); }elseif(param.message==='orderPractice') {// 跳转到顺序练习const param: ROUTE_PARAM = { title: '顺序练习',type:EXAM_MANAGER_TYPE.sequence, };CommonModel.instance.navStack.replacePathByName('practiceView',param); } }WantUtils.handlePushWant(want); this.shareServiceImpl.handleWant(want,this.context); }

6.2 冷启动与热启动处理

推送点击有两种场景:

  1. 冷启动:应用未运行,点击通知启动应用 → 在onCreate中处理
  2. 热启动:应用已在后台,点击通知唤醒应用 → 在onNewWant中处理
// 冷启动处理onCreate(want: Want,launchParam: AbilityConstant.LaunchParam){Logger.info(TAG, 'Ability onCreate');WantUtils.handlePushCall(want);// 处理冷启动推送this.shareServiceImpl.handleWant(want,this.context); }// 热启动处理onNewWant(want: Want,launchParam: AbilityConstant.LaunchParam): void {// 处理热启动推送WantUtils.handlePushWant(want); this.shareServiceImpl.handleWant(want,this.context); }

七、推送功能的扩展

7.1 本地通知

除了服务器推送,应用还可以发送本地通知,例如模拟考试完成后的成绩通知:

staticsendLocalNotification(title:string, content:string, context: Context) {constnotificationRequest: notificationManager.NotificationRequest= {id:Date.now(),content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: title,text: content, }, }, }; notificationManager.publish(notificationRequest) .then(() =>Logger.info('PushUtils','Local notification published')) .catch((err) =>Logger.error('PushUtils','Failed to publish: '+ err.message)); }

7.2 推送分类

不同类型的推送可以使用不同的通知渠道:

// 通知渠道分类const NOTIFICATION_CHANNELS = { EXAM_REMINDER: { id:'exam_reminder',name:'考试提醒',importance:4}, STUDY_TIP: { id:'study_tip',name:'学习建议',importance:3}, PROMOTION: { id:'promotion',name:'活动推广',importance:2}, };

八、总结

推送服务是应用与用户保持互动的重要渠道。DriverLicenseExam 项目通过完整的推送实现展示了:

  1. 权限管理:通知权限的申请和检查
  2. 消息构建:NotificationRequest 的消息结构
  3. 推送发送:通过 notificationManager.publish 发送
  4. 点击处理:通过 Want 参数传递,支持冷启动和热启动
  5. 页面跳转:根据推送类型跳转到不同页面

关键源码文件:

  • commons/commonLib/src/main/ets/push/PushUtils.ets— 推送工具类
  • commons/commonLib/src/main/ets/push/Model.ets— 推送数据模型
  • products/entry/src/main/ets/entryability/EntryAbility.ets— 推送点击处理
  • products/entry/src/main/ets/pages/MainEntry.ets— 推送触发入口
  • products/entry/src/main/ets/util/WantUtils.ets— Want 参数处理工具
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/18 17:32:29

常用git指令

//拉取最新代码,如果你既不想提交也不想暂存更改,但仍然需要继续工作,你可以尝试使用 --no-rebase 参数来执行普通的合并操作 git pull --no-rebase//add需要添加的文件,可在编辑器便捷操作 git add ~~~,git add -A//c…

作者头像 李华
网站建设 2026/8/18 17:29:45

VBrowser-Android多线程下载原理:分片下载与文件合并的完整实现

VBrowser-Android多线程下载原理:分片下载与文件合并的完整实现 【免费下载链接】VBrowser-Android 全网视频嗅探缓存APP 项目地址: https://gitcode.com/gh_mirrors/vb/VBrowser-Android VBrowser-Android 是一款开源的「全网视频嗅探缓存APP」,…

作者头像 李华
网站建设 2026/8/18 17:28:23

CKAN模组管理终极指南:3步告别KSP手动装MOD的噩梦

CKAN模组管理终极指南:3步告别KSP手动装MOD的噩梦 【免费下载链接】CKAN The Comprehensive Kerbal Archive Network 项目地址: https://gitcode.com/gh_mirrors/cka/CKAN 上周我朋友痛骂了坎巴拉太空计划整整一天。原因是他的KSP存档里,一架耗尽…

作者头像 李华
网站建设 2026/8/18 17:23:35

ComfyUI 完整入门指南:从零打造可视化 AI 图像视频生成工作流

ComfyUI 完整入门指南:从零打造可视化 AI 图像视频生成工作流 【免费下载链接】ComfyUI The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. 项目地址: https://gitcode.com/GitHub_Trending/co/ComfyUI 如…

作者头像 李华
网站建设 2026/8/18 17:12:40

收藏!AI大模型人才抢手,小白也能抓住高薪机遇,小白必看!

文章指出,尽管科技巨头因AI裁员,但AI人才需求激增,尤其是大模型相关岗位。数据显示,AI岗位增速远超整体校招,全行业都在争抢AI人才。文章还介绍了AI领域的四大主流赛道:AIGC视觉设计、AI云计算、AI大模型开…

作者头像 李华