MobPush 鸿蒙端 SDK API
获取 RID 获取注册 id(可与用户 id 绑定,实现向指定用户推送消息)
export function getRegistrationId(callback: AsyncCallback<string>): void
export function getRegistrationId(): Promise<string>
//示例代码:let getRegistrationIdCallback = (err: Base.BusinessError, data: string): void => {let message = ""if (err) {message = 获取RID失败, code is ${err.code}, message is ${err.message}
;} else {message = "RID:" + data;}console.log(message)}mobPush.getRegistrationId(getRegistrationIdCallback)
mobPush.getRegistrationId().then((data: string) => {console.log(RID: ${data}
)})推送监听设置推送监听建议在 AbilityStage 中设置
export function addPushReceiver(receiver: MobPushReceiver)
//示例代码:let receive: mobPush.MobPushReceiver = {onCustomMessageReceive: (message: mobPush.MobPushCustomMessage) => {//接收到自定义消息(透传消息)message.getMessageId();//获取任务 IDmessage.getContent();//获取推送内容...},onNotifyMessageReceive: (message: mobPush.MobPushNotifyMessage) => {//通知消息到达 message.getMobNotifyId();//获取消息 IDmessage.getMessageId();//获取任务 IDmessage.getTitle();//获取推送标题 message.getContent();//获取推送内容...},onNotifyMessageOpenedReceive: (message: mobPush.MobPushNotifyMessage) => {//通知被点击事件//需设置 mobPush.notificationClickAck()message.getMobNotifyId();//获取消息 IDmessage.getMessageId();//获取任务 IDmessage.getTitle();//获取推送标题 message.getContent();//获取推送内容...},//type=1:RID 更新 type=2:厂商 deviceToken 更新 onCommandReceive: (type: number, map: HashMap<string, Object>) => {//channel:mobpush/harmonylet channel = map.get(mobPush.KEY_CHANNEL)//对应 channel 的更新 tokenlet token = map.get(mobPush.KEY_TOKEN)}}mobPush.addPushReceiver(receive)移除推送监听 export function removePushReceiver(receiver: MobPushReceiver)
//示例代码:mobPush.removePushReceiver(receive)别名设置别名 errorCode 为操作结果(0 成功,其他失败)
export function setAlias(alias: string, callback: AsyncCallback<AliasResult>): void
export function setAlias(alias: string): Promise<AliasResult>
//示例代码
let setAliasCallback = (err: Base.BusinessError, data: mobPush.AliasResult): void => {console.log("setAlias:" + JSON.stringify(data))console.log("alias:" + data.alias)console.log("errCode:" + data.errorCode)}mobPush.setAlias("alias", setAliasCallback)
mobPush.setAlias("alias").then((data: mobPush.AliasResult) => {console.log("setAlias:" + JSON.stringify(data))console.log("alias:" + data.alias)console.log("errCode:" + data.errorCode)})获取别名 errorCode 为操作结果(0 成功,其他失败)
export function getAlias(callback: AsyncCallback<AliasResult>): void
export function getAlias(): Promise<AliasResult>
//示例代码
let getAliasCallback = (err: Base.BusinessError, data: mobPush.AliasResult): void => {console.log("getAlias:" + JSON.stringify(data))console.log("alias:" + data.alias)console.log("errCode:" + data.errorCode)}mobPush.getAlias(getAliasCallback)
mobPush.getAlias().then((data: mobPush.AliasResult) => {console.log("getAlias:" + JSON.stringify(data))console.log("alias:" + data.alias)console.log("errCode:" + data.errorCode)})删除别名 errorCode 为操作结果(0 成功,其他失败)
export function deleteAlias(callback: AsyncCallback<AliasResult>): void
export function deleteAlias(): Promise<AliasResult>
//示例代码
let getAliasCallback = (err: Base.BusinessError, data: mobPush.AliasResult): void => {console.log("deleteAlias:" + JSON.stringify(data))console.log("alias:" + data.alias)console.log("errCode:" + data.errorCode)}mobPush.deleteAlias(getAliasCallback)
mobPush.deleteAlias().then((data: mobPush.AliasResult) => {console.log("deleteAlias:" + JSON.stringify(data))console.log("alias:" + data.alias)console.log("errCode:" + data.errorCode)})标签添加标签 errorCode 为操作结果(0 成功,其他失败)
export function addTags(tags: string[], callback: AsyncCallback<TagsResult>): void
export function addTags(tags: string[]): Promise<TagsResult>
//示例代码 let addTagsCallback = (err: Base.BusinessError, data: mobPush.TagsResult): void => {console.log("addTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)}mobPush.addTags(["tag"] , addTagsCallback)
mobPush.addTags(["tag"]).then((data: mobPush.TagsResult) => {console.log("addTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)})获取标签 errorCode 为操作结果(0 成功,其他失败)
export function getTags(callback: AsyncCallback<TagsResult>): void
export function getTags(): Promise<TagsResult>
//示例代码 let getTagsCallback = (err: Base.BusinessError, data: mobPush.TagsResult): void => {console.log("addTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)}mobPush.getTags(getTagsCallback)
mobPush.getTags().then((data: mobPush.TagsResult) => {console.log("getTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)})删除标签 errorCode 为操作结果(0 成功,其他失败)
export function deleteTags(tags: string[], callback: AsyncCallback<TagsResult>): void
export function deleteTags(tags: string[]): Promise<TagsResult>
//示例代码 let deleteTagsCallback = (err: Base.BusinessError, data: mobPush.TagsResult): void => {console.log("deleteTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)}mobPush.deleteTags(["tag"] , deleteTagsCallback)
mobPush.deleteTags(["tag"]).then((data: mobPush.TagsResult) => {console.log("deleteTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)})清空所有标签 errorCode 为操作结果(0 成功,其他失败)
export function cleanTags(callback: AsyncCallback<TagsResult>): void
export function cleanTags(): Promise<TagsResult>
//示例代码 let cleanTagsCallback = (err: Base.BusinessError, data: mobPush.TagsResult): void => {console.log("cleanTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)}mobPush.cleanTags(cleanTagsCallback)
mobPush.cleanTags().then((data: mobPush.TagsResult) => {console.log("cleanTags:" + JSON.stringify(data))console.log("tags:" + data.tags)console.log("errCode:" + data.errorCode)})角标设置是否显示角标用于接收通知时显示角标数量
export function setShowBadge(show: boolean)
//示例代码//设置显示角标 mobPush.setShowBadge(true)//设置不显示角标 mobPush.setShowBadge(false)获取是否显示角标 export function getShowBadge(): boolean
//示例代码 let isShow = mobPush.getShowBadge())console.log("角标显示:" + isShow)设置角标数设置显示的角标数
export function setBadgeCounts(count: number)
//示例代码 mobPush.setBadgeCounts(99)通用通知点击回执上报须在通知打开的 UIAbility 中调用 否则会无点击回调
export function notificationClickAck(want: Want): void
//示例代码 export default class EntryAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');console.log("EntryAbility onCreate")mobPush.notificationClickAck(want)if (want) {if (want.parameters) {//获取推送设置的额外内容 if (want.parameters["pushData"]) {console.log("pushData:" + JSON.stringify((want.parameters["pushData"])))}}}}
}
onWindowStageCreate(windowStage: window.WindowStage): void {// Main window is created, set main page for this abilityhilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
}
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {console.log("EntryAbility onNewWant")mobPush.notificationClickAck(want)if (want) {if (want.parameters) {//获取推送设置的额外内容 if (want.parameters["pushData"]) {console.log("pushData:" + JSON.stringify((want.parameters["pushData"])))}}}}}停止推送停止后将不会收到推送消息,仅可通过 restartPush 重新打开
export function stopPush()
//示例代码:mobPush.stopPush()重新打开推送服务 export function restartPush()
//示例代码:mobPush.restartPush()判断推送服务是否已经停止 export function isPushStopped(): boolean
//示例代码:let isStopped = mobPush.isPushStopped()console.log("isStopped:" + isStopped)清除所有通知 export function clearAllNotification()
//示例代码 mobPush.clearAllNotification()获取厂商 tokenexport function getDeviceToken(): string
//示例代码 let token = mobPush.getDeviceToken()console.log("厂商 token:" + token)判断是否开启通知权限 export function isNotificationEnabled(callback: AsyncCallback<boolean>): void
export function isNotificationEnabled(): Promise<boolean>
//示例代码 let isNotificationEnabledCallback = (err: Base.BusinessError, data: boolean): void => {let message = ""if (err) {message = 获取通知权限是否开启失败, code is ${err.code}, message is ${err.message}
;} else {message = 通知权限是否开启: ${JSON.stringify(data)}
;}console.log(message)}mobPush.isNotificationEnabled(isNotificationEnabledCallback)
mobPush.isNotificationEnabled().then((data: boolean) => {console.log(通知权限是否开启: ${data}
)})检测推送 tcp 连接状态 export function checkTcpStatus(callback: Callback<boolean>): void
export function checkTcpStatus(): Promise<boolean>
//示例代码 let tcpStatusCallback = (data: boolean): void => {let message = TCP状态: ${JSON.stringify(data)}
;console.log(message)}mobPush.checkTcpStatus(tcpStatusCallback)
mobPush.checkTcpStatus().then((data: boolean) => {console.log(TCP状态: ${data}
)})
版权声明: 本文为 InfoQ 作者【MobTech袤博科技】的原创文章。
原文链接:【http://xie.infoq.cn/article/9656e10157d41b9e815b1649f】。文章转载请联系作者。
评论