青州市网站建设,wordpress文章内多页面,网站建设招标公示,资讯类网站开发文档一. 服务简述 Push Kit#xff08;推送服务#xff09;是华为提供的消息推送平台#xff0c;建立了从云端到终端的消息推送通道。所有HarmonyOS 应用可通过集成 Push Kit#xff0c;实现向应用实时推送消息#xff0c;使消息易见#xff0c;构筑良好的用户关系#xff0…一. 服务简述 Push Kit推送服务是华为提供的消息推送平台建立了从云端到终端的消息推送通道。所有HarmonyOS 应用可通过集成 Push Kit实现向应用实时推送消息使消息易见构筑良好的用户关系提升用户的感知度和活跃度。
二. 开发准备
1在华为开发者联盟网站注册成为开发者并完成实名认证。
2参考调试应用与发布应用添加 APP ID、创建证书、添加调试设备、创建 Profile 文件。
3在 AppGallery Connect 上参考创建项目与创建应用中完成 HarmonyOS 应用的创建。
4参考配置应用签名证书指纹添加公钥指纹。
三. 工程配置
1在应用 entry 模块的 module.json5 中添加 metadata - client_idclient_id 在 AppGallery Connect - 我的项目中查看。
{module: {name: entry,...metadata: [{name: client_id,value: 11659886}]}
}2在应用 entry 模块的 module.json5 中添加 abilities - skills - actions。
{module: {name: entry,...abilities: [{name: EntryAbility,...skills: [{actions: [action.system.home,ohos.want.action.viewData]}, {actions: [action.ohos.push.listener, // 订阅场景消息IM | VoIP | BACKGROUND | EMERGENCYcom.base.action.pushService // 与服务端共同协议的action]}]}]}
}四. 功能实现
1. 获取pushToken
import { pushService } from kit.PushKit;export class PushManager {public getPushToken(): Promisestring {return pushService.getToken()}
}
2. 通知权限申请
import notificationManager from ohos.notificationManager;export class PushManager {public async checkNotificationPermissions(): Promisevoid {let grantStatus await notificationManager.isNotificationEnabled()if (!grantStatus) {await notificationManager.requestEnableNotification()grantStatus await notificationManager.isNotificationEnabled()if (!grantStatus) {console.log(通知权限未开启)} else {console.log(通知权限已开启)}} else {console.log(通知权限已开启)}}
}
3. 消息接收/处理
1冷启动
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {super.onCreate(want, launchParam)if (want want.action com.base.action.pushService) {console.log(push_want_parameters: ${JSON.stringify(want.parameters)})PushManager.getInstance().onCreate(want)}
}
2后台唤起
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {super.onNewWant(want, launchParam)if (want want.action com.base.action.pushService) {console.log(push_want_parameters: ${JSON.stringify(want.parameters)})PushManager.getInstance().onNewWant(want)}
}
4. 订阅场景消息
场景化消息类型包含IM通知扩展消息场景、VoIP应用内通话消息场景、BACKGROUND后台消息场景、EMERGENCY紧急事件消息场景。
import { pushService, pushCommon } from kit.PushKit;export class PushManager {public receiveMessage(pushType: IM | VoIP | BACKGROUND | EMERGENCY): void {const ability AppStorage.get(ability) as UIAbility // 在EntryAbility中存储AbilitypushService.receiveMessage(pushType, ability, (payload: pushCommon.PushPayload) {const data: PushPayloadData JSON.parse(payload.data) as PushPayloadDataconsole.log(pushService_receiveMessage_data : ${JSON.stringify(data)});})}
}
5. 推送错误码
调用推送相关的API报错可以参考推送服务API错误码。