jpushService.js 5.72 KB
Newer Older
Nature's avatar
Nature committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/**
 * Created by LJh on 2017/4/13.
 */
export default {
// 获取设备号
  onGetRegistrationID: function (data) {
    try {
      // alert("JPushPlugin:registrationID is " + JSON.stringify(data));
      if (data.length !== 0) {
        window.localStorage.setItem('registrationID', data)
      }
      if (data.length === 0) {
        window.setTimeout(this.getRegistrationID, 1000)
      }
    } catch (exception) {
      // alert(angular.toJson(exception));
    }
  },
  // 获取设备号
  getRegistrationID: function () {
    window.plugins.jPushPlugin.getRegistrationID(this.onGetRegistrationID)
  },
  onReceiveNotification: function (event) {
    try {
      let alertContent // eslint-disable-line
      if (device.platform === 'Android') { // eslint-disable-line
        alertContent = event.alert
      } else {
        alertContent = event.aps.alert
      }
    } catch (exception) {
      console.log(exception)
    }
  },

  onReceiveMessage: function (event) {
    try {
      let message // eslint-disable-line
      if (device.platform === 'Android') { // eslint-disable-line
        message = event.message
      } else {
        message = event.content
      }
    } catch (exception) {
      // console.log("JPushPlugin:onReceiveMessage-->" + exception);
    }
  },

  onOpenNotification: function (event) {
    window.plugins.jPushPlugin.resetBadge()// 设置Badge
    window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)
    try {
      let alertContent // eslint-disable-line
      if (device.platform === 'Android') { // eslint-disable-line
        alertContent = event.alert
      } else {
        alertContent = event.aps.alert
      }
      // alert("open Notification:" + JSON.stringify(event,'',2));
    } catch (exception) {
      // console.log("JPushPlugin:onOpenNotification" + exception);
    }
  },
  // 启动极光推送
  _init: function () {
    if (process.env.isMobilePlatform) {
      window.plugins.jPushPlugin.init()
      window.plugins.jPushPlugin.setDebugMode(false)
      window.plugins.jPushPlugin.getRegistrationID(this.onGetRegistrationID)
      document.addEventListener('jpush.openNotification', this.onOpenNotification)
      window.plugins.jPushPlugin.resetBadge()// 设置Badge
      window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)
      // document.addEventListener('jpush.receiveMessage', this.onReceiveMessage);
      // document.addEventListener('jpush.receiveNotification', this.onReceiveNotification);
    }
  },

  /*
   * 获取状态
   * */
  _isPushStopped: function (fun) {
    window.plugins.jPushPlugin.isPushStopped(fun)
  },
  /*
   * 停止极光推送
   * */
  _stopPush: function () {
    window.plugins.jPushPlugin.stopPush()
  },
  /*
   * 重启极光推送
   * */
  _resumePush: function () {
    window.plugins.jPushPlugin.resumePush()
  },

  /**
   * 设置标签。
   * 注意:该接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
   *
   * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
   */
  _setTags: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.setTags(params, successCallback, errorCallback)
  },
  /**
   * 新增标签。
   *
   * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
   */
  _addTags: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.addTags(params, successCallback, errorCallback)
  },
  /**
   * 删除指定标签。
   *
   * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
   */
  _deleteTags: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.deleteTags(params, successCallback, errorCallback)
  },
  /**
   * 清除标签
   * @param params
   * @param successCallback
   * @param errorCallback
   * @private
   */
  _cleanTags: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.cleanTags(params, successCallback, errorCallback)
  },
  /**
   * 查询所有标签。
   *
   * @param params = { 'sequence': number }
   */
  _getAllTags: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.getAllTags(params, successCallback, errorCallback)
  },
  /*
   * 设置别名
   * */
  _setAlias: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.setAlias(params, successCallback, errorCallback)
  },
  /**
   * 删除别名。
   *
   * @param params = { 'sequence': number }
   */
  _deleteAlias: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.deleteAlias(params, successCallback, errorCallback)
  },
  /**
   * 查询当前绑定的别名。
   *
   * @param params = { 'sequence': number }
   */
  _getAlias: function (params, successCallback, errorCallback) {
    window.plugins.jPushPlugin.getAlias(params, successCallback, errorCallback)
  },
  /**
   * 设置角标
   * @param value
   * @private
   */
  _setBadge: function (value) {
    window.plugins.jPushPlugin.setBadge(value)
  },
  /*
   * 清除角标
   * */
  _resetBadge: function () {
    window.plugins.jPushPlugin.resetBadge()
    window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0)
  },

  _setLogOFF: function () {
    window.plugins.jPushPlugin.setLogOFF()
  },

  // 极光推送
  _jpush_config: function (tagOption) {
    if (process.env.isMobilePlatform) {
      let userId = window.localStorage.user_id
      let tags = []
      for (let key in tagOption) {
        tags.push(tagOption[key])
      }
      /* if (userName) {
        tags.push(userName.toUpperCase())
      } */
      window.plugins.jPushPlugin.setTags({sequence: Number(userId), tags: tags},
        function (result) {
          // alert(result.tags)
        }, function (error) {
          // alert(error.code)
        })
    }
  },
}