MobPush 鸿蒙端SDK API 原创

MobService
发布于 2024-7-2 18:09
浏览
0收藏

获取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();//获取任务ID
         message.getContent();//获取推送内容
         ...
      },
      onNotifyMessageReceive:message: mobPush.MobPushNotifyMessage) => {
        //通知消息到达
        message.getMobNotifyId();//获取消息ID
        message.getMessageId();//获取任务ID
        message.getTitle();//获取推送标题
        message.getContent();//获取推送内容
        ...
      },
      onNotifyMessageOpenedReceive:message: mobPush.MobPushNotifyMessage) => {
        //通知被点击事件 
        //需设置mobPush.notificationClickAck()
        message.getMobNotifyId();//获取消息ID
        message.getMessageId();//获取任务ID
        message.getTitle();//获取推送标题
        message.getContent();//获取推送内容
        ...
      },
      //type=1:RID更新  type=2:厂商deviceToken更新
      onCommandReceive:type: number, map: HashMap<string, Object>) => {
        //channel:mobpush/harmony
        let channel = map.get(mobPush.KEY_CHANNEL)
        //对应channel的更新token
        let 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:")

删除别名

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:")

标签

添加标签

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:")

获取标签

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:")

删除标签

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:")

清空所有标签

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:")

角标

设置是否显示角标

用于接收通知时显示角标数量

export function setShowBadge(show: boolean)

//示例代码 
//设置显示角标
mobPush.setShowBadge(true)
//设置不显示角标
mobPush.setShowBadge(false)

获取是否显示角标

export function getShowBadge(): boolean

//示例代码= mobPush.getShowBadge())
console.log("角标显示:"

设置角标数

设置显示的角标数

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 ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  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 

//示例代码:= mobPush.isPushStopped()
console.log("isStopped:"

清除所有通知

export function clearAllNotification()

//示例代码
mobPush.clearAllNotification()

获取厂商token

export 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}`)
})


©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏
回复
举报
回复
    相关推荐