PushContentParser.ets 5.53 KB
import Want from '@ohos.app.ability.Want';
import { Logger } from 'wdKit/Index';
import { JSON } from '@kit.ArkTS';
import { AppInnerLink } from 'wdRouter/Index';

export interface PushContentBean {
  isPush: boolean   // 是否为推送数据
  want?: Want       // want参数 (用来在消费时,回执)
  online: boolean   // 解析want,是否为在线消息(在线走的是个推通道,离线走的是华为厂商通道)
  pushLink?: string // 解析want,对应pushLink参数
  notifyTitle?: string
  notifyContent?: string


  // 透传消息发送的本地测试通知
  localNotify?:boolean
  taskId?: string
  messageId?: string
}

/*

 * */
export class PushContentParser {


  private static LAUNCH_PARAM_GETUI_TRANSMIT_MSG_LOCAL_NOTIFY = "gtTransmitMsgLocalNotify"
  private static LAUNCH_PARAM_GETUI_DATA = "gtdata"
  private static LAUNCH_PARAM_GETUI_TASKID = "taskid"
  private static LAUNCH_PARAM_GETUI_NOTIFYID = "_push_notifyid"
  private static PUSH_PARAM_PUSH_LINK = "pushLink"

  static getPushLinkFromWant(want: Want) : PushContentBean {
    // 个推在线消息
    if (want && want.parameters && want.parameters[PushContentParser.LAUNCH_PARAM_GETUI_DATA]) {
      /*
       {
        "age": 12,
        "gtdata": {
            "actionid": "10010",
            "appid": "ZMi5AAXYHE84VN9p55YpW2",
            "bigStyle": 0,
            "checkpackage": "com.peopledailychina.hosactivity",
            "content": "通知内容444",
            "feedbackid": "0",
            "isFloat": false,
            "messageid": "44fd0876c9834214a6b7032b35d9826b",
            "notifID": 62288,
            "notifyStyle": 0,
            "payload": "",
            "taskid": "TEST_0516_03a84918e7df6191502497ed2cbef384",
            "title": "测试通知444",
            "wantUri": "{\"deviceId\":\"\",\"bundleName\":\"com.peopledailychina.hosactivity\",\"abilityName\":\"EntryAbility\",\"uri\":\"rmrbapp://rmrb.app:8080/openwith\",\"action\":\"com.test.pushaction\",\"parameters\":{\"pushLink\":\"Getui\",\"age\":12}}"
        },
        "isCallBySCB": false,
        "moduleName": "phone",
        "pushLink": "Getui",
        "ohos.aafwk.param.callerAbilityName": "",
        "ohos.aafwk.param.callerBundleName": "com.ohos.sceneboard",
        "ohos.aafwk.param.callerPid": 44239,
        "ohos.aafwk.param.callerToken": 537702494,
        "ohos.aafwk.param.callerUid": 20020018,
        "specifyTokenId": 537063794
    }
      },*/
      let gtData = want.parameters[PushContentParser.LAUNCH_PARAM_GETUI_DATA] as Record<string, string | number | object>
      if (gtData[PushContentParser.LAUNCH_PARAM_GETUI_TASKID] != undefined) {
        // let json = JSON.parse(gtData["wantUri"] as string) as Record<string, string | number>
        // if (json && json[PushContentParser.PUSH_PARAM_PUSH_LINK] != null) {
        //   const pushLink = json[PushContentParser.PUSH_PARAM_PUSH_LINK] as string
        //   return {
        //     isPush: true, online: true, pushLink: pushLink, want: want
        //   }
        // }
        if (want.parameters[PushContentParser.PUSH_PARAM_PUSH_LINK]) {
          let pushLink = want.parameters[PushContentParser.PUSH_PARAM_PUSH_LINK] as string
          return {
            isPush: true, online: true, pushLink: pushLink, want: want,
            notifyTitle: gtData["title"] as string,
            notifyContent: gtData["content"] as string,
          }
        }
      }
    }

    /*
    {
    "deviceId": "",
    "bundleName": "com.peopledailychina.hosactivity",
    "abilityName": "EntryAbility",
    "moduleName": "phone",
    "uri": "rmrbapp://rmrb.app:8080/openwith",
    "type": "",
    "flags": 0,
    "action": "com.test.pushaction",
    "parameters": {
        "_push_notifyid": 1,
        "age": 12,  // 自定义
        "debugApp": false,
        "isCallBySCB": false,
        "moduleName": "phone",
        "pushLink": "Getui", // 自定义
        "ohos.aafwk.param.callerAbilityName": "PushServiceInnerAbility",
        "ohos.aafwk.param.callerBundleName": "com.huawei.hms.pushservice",
        "ohos.aafwk.param.callerPid": 22808,
        "ohos.aafwk.param.callerToken": 537908725,
        "ohos.aafwk.param.callerUid": 20004,
        "ohos.dlp.params.sandbox": false
    },
    "entities": [

    ]
     * */
    // 离线消息,华为直接推送
    if (want && want.parameters && want.parameters[PushContentParser.LAUNCH_PARAM_GETUI_NOTIFYID]) {
      if (want.parameters[PushContentParser.PUSH_PARAM_PUSH_LINK]) {
        let pushLink = want.parameters[PushContentParser.PUSH_PARAM_PUSH_LINK] as string
        return {
          isPush: true, online: false, pushLink: pushLink, want: want,
          notifyTitle: want.parameters["title"] as string,
          notifyContent: want.parameters["content"] as string,
        }
      }
    }


    // 本地发送的测试通知
    if (want && want.parameters && want.parameters[PushContentParser.LAUNCH_PARAM_GETUI_TRANSMIT_MSG_LOCAL_NOTIFY]) {
      if (want.parameters[PushContentParser.PUSH_PARAM_PUSH_LINK]) {
        let pushLink = want.parameters[PushContentParser.PUSH_PARAM_PUSH_LINK] as string
        return {
          isPush: true, online: true, pushLink: pushLink, want: want,
          notifyTitle: want.parameters["title"] as string,
          notifyContent: want.parameters["content"] as string,
          localNotify: true,
          taskId: want.parameters["taskId"] as string,
          messageId: want.parameters["messageId"] as string,
        }
      }
    }
    return {
      isPush: false, online: false
    }
  }

  static jumpWithPushLink(pushLink: string) {
    AppInnerLink.jumpWithLink(pushLink)
  }
}