JPushPlugin.js 11.2 KB
Newer Older
李晓兵's avatar
李晓兵 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
var JPushPlugin = function () {}

// private plugin function

JPushPlugin.prototype.receiveMessage = {}
JPushPlugin.prototype.openNotification = {}
JPushPlugin.prototype.receiveNotification = {}

JPushPlugin.prototype.isPlatformIOS = function () {
  return (device.platform === 'iPhone' ||
    device.platform === 'iPad' ||
    device.platform === 'iPod touch' ||
    device.platform === 'iOS')
}

JPushPlugin.prototype.errorCallback = function (msg) {
  console.log('JPush Callback Error: ' + msg)
}

JPushPlugin.prototype.callNative = function (name, args, successCallback, errorCallback) {
  if (errorCallback) {
    cordova.exec(successCallback, errorCallback, 'JPushPlugin', name, args)
  } else {
    cordova.exec(successCallback, this.errorCallback, 'JPushPlugin', name, args)
  }
}

// Common methods
JPushPlugin.prototype.init = function () {
  if (this.isPlatformIOS()) {
    this.callNative('initial', [], null)
  } else {
    this.callNative('init', [], null)
  }
}

JPushPlugin.prototype.setDebugMode = function (mode) {
  if (device.platform === 'Android') {
    this.callNative('setDebugMode', [mode], null)
  } else {
    if (mode === true) {
      this.setDebugModeFromIos()
    } else {
      this.setLogOFF()
    }
  }
}

JPushPlugin.prototype.getRegistrationID = function (successCallback) {
  this.callNative('getRegistrationID', [], successCallback)
}

JPushPlugin.prototype.stopPush = function () {
  this.callNative('stopPush', [], null)
}

JPushPlugin.prototype.resumePush = function () {
  this.callNative('resumePush', [], null)
}

JPushPlugin.prototype.isPushStopped = function (successCallback) {
  this.callNative('isPushStopped', [], successCallback)
}

JPushPlugin.prototype.clearLocalNotifications = function () {
  if (device.platform === 'Android') {
    this.callNative('clearLocalNotifications', [], null)
  } else {
    this.clearAllLocalNotifications()
  }
}

/**
 * 设置标签。
 * 注意:该接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
 * 
 * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
 */
JPushPlugin.prototype.setTags = function (params, successCallback, errorCallback) {
  this.callNative('setTags', [params], successCallback, errorCallback)
}

/**
 * 新增标签。
 * 
 * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
 */
JPushPlugin.prototype.addTags = function (params, successCallback, errorCallback) {
  this.callNative('addTags', [params], successCallback, errorCallback)
}

/**
 * 删除指定标签。
 * 
 * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
 */
JPushPlugin.prototype.deleteTags = function (params, successCallback, errorCallback) {
  this.callNative('deleteTags', [params], successCallback, errorCallback)
}

/**
 * 清除所有标签。
 * 
 * @param params = { 'sequence': number }
 */
JPushPlugin.prototype.cleanTags = function (params, successCallback, errorCallback) {
  this.callNative('cleanTags', [params], successCallback, errorCallback)
}

/**
 * 查询所有标签。
 * 
 * @param params = { 'sequence': number }
 */
JPushPlugin.prototype.getAllTags = function (params, successCallback, errorCallback) {
  this.callNative('getAllTags', [params], successCallback, errorCallback)
}

/**
 * 查询指定标签与当前用户的绑定状态。
 * 
 * @param params = { 'sequence': number, 'tag': string }
 */
JPushPlugin.prototype.checkTagBindState = function (params, successCallback, errorCallback) {
  this.callNative('checkTagBindState', [params], successCallback, errorCallback)
}

/**
 * 设置别名。
 * 注意:该接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
 * 
 * @param params = { 'sequence': number, 'alias': string }
 */
JPushPlugin.prototype.setAlias = function (params, successCallback, errorCallback) {
  this.callNative('setAlias', [params], successCallback, errorCallback)
}

/**
 * 删除别名。
 * 
 * @param params = { 'sequence': number }
 */
JPushPlugin.prototype.deleteAlias = function (params, successCallback, errorCallback) {
  this.callNative('deleteAlias', [params], successCallback, errorCallback)
}

/**
 * 查询当前绑定的别名。
 * 
 * @param params = { 'sequence': number }
 */
JPushPlugin.prototype.getAlias = function (params, successCallback, errorCallback) {
  this.callNative('getAlias', [params], successCallback, errorCallback)
}

// 判断系统设置中是否对本应用启用通知。
// iOS: 返回值如果大于 0,代表通知开启;0: 通知关闭。
// UIRemoteNotificationTypeNone = 0,
// UIRemoteNotificationTypeBadge = 1 << 0,
// UIRemoteNotificationTypeSound = 1 << 1,
// UIRemoteNotificationTypeAlert = 1 << 2,
// UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3,
// Android: 返回值 1 代表通知启用;0: 通知关闭。
JPushPlugin.prototype.getUserNotificationSettings = function (successCallback) {
  if (this.isPlatformIOS()) {
    this.callNative('getUserNotificationSettings', [], successCallback)
  } else if (device.platform === 'Android') {
    this.callNative('areNotificationEnabled', [], successCallback)
  }
}

// iOS methods

JPushPlugin.prototype.startJPushSDK = function () {
  this.callNative('startJPushSDK', [], null)
}

JPushPlugin.prototype.setBadge = function (value) {
  if (this.isPlatformIOS()) {
    this.callNative('setBadge', [value], null)
  }
}

JPushPlugin.prototype.resetBadge = function () {
  if (this.isPlatformIOS()) {
    this.callNative('resetBadge', [], null)
  }
}

JPushPlugin.prototype.setDebugModeFromIos = function () {
  if (this.isPlatformIOS()) {
    this.callNative('setDebugModeFromIos', [], null)
  }
}

JPushPlugin.prototype.setLogOFF = function () {
  if (this.isPlatformIOS()) {
    this.callNative('setLogOFF', [], null)
  }
}

JPushPlugin.prototype.setCrashLogON = function () {
  if (this.isPlatformIOS()) {
    this.callNative('crashLogON', [], null)
  }
}

JPushPlugin.prototype.addLocalNotificationForIOS = function (delayTime, content,
  badge, notificationID, extras) {
  if (this.isPlatformIOS()) {
    this.callNative('setLocalNotification', [delayTime, content, badge, notificationID, extras], null)
  }
}

JPushPlugin.prototype.deleteLocalNotificationWithIdentifierKeyInIOS = function (identifierKey) {
  if (this.isPlatformIOS()) {
    this.callNative('deleteLocalNotificationWithIdentifierKey', [identifierKey], null)
  }
}

JPushPlugin.prototype.clearAllLocalNotifications = function () {
  if (this.isPlatformIOS()) {
    this.callNative('clearAllLocalNotifications', [], null)
  }
}

JPushPlugin.prototype.setLocation = function (latitude, longitude) {
  if (this.isPlatformIOS()) {
    this.callNative('setLocation', [latitude, longitude], null)
  }
}

JPushPlugin.prototype.startLogPageView = function (pageName) {
  if (this.isPlatformIOS()) {
    this.callNative('startLogPageView', [pageName], null)
  }
}

JPushPlugin.prototype.stopLogPageView = function (pageName) {
  if (this.isPlatformIOS()) {
    this.callNative('stopLogPageView', [pageName], null)
  }
}

JPushPlugin.prototype.beginLogPageView = function (pageName, duration) {
  if (this.isPlatformIOS()) {
    this.callNative('beginLogPageView', [pageName, duration], null)
  }
}

JPushPlugin.prototype.setApplicationIconBadgeNumber = function (badge) {
  if (this.isPlatformIOS()) {
    this.callNative('setApplicationIconBadgeNumber', [badge], null)
  }
}

JPushPlugin.prototype.getApplicationIconBadgeNumber = function (callback) {
  if (this.isPlatformIOS()) {
    this.callNative('getApplicationIconBadgeNumber', [], callback)
  }
}

JPushPlugin.prototype.addDismissActions = function (actions, categoryId) {
  this.callNative('addDismissActions', [actions, categoryId])
}

JPushPlugin.prototype.addNotificationActions = function (actions, categoryId) {
  this.callNative('addNotificationActions', [actions, categoryId])
}

// Android methods
JPushPlugin.prototype.getConnectionState = function (successCallback) {
  if (device.platform === 'Android') {
    this.callNative('getConnectionState', [], successCallback)
  }
}

JPushPlugin.prototype.setBasicPushNotificationBuilder = function () {
  if (device.platform === 'Android') {
    this.callNative('setBasicPushNotificationBuilder', [], null)
  }
}

JPushPlugin.prototype.setCustomPushNotificationBuilder = function () {
  if (device.platform === 'Android') {
    this.callNative('setCustomPushNotificationBuilder', [], null)
  }
}

JPushPlugin.prototype.receiveRegistrationIdInAndroidCallback = function (data) {
  if (device.platform === 'Android') {
    data = JSON.stringify(data)
    var event = JSON.parse(data)
    cordova.fireDocumentEvent('jpush.receiveRegistrationId', event)
  }
}

JPushPlugin.prototype.receiveMessageInAndroidCallback = function (data) {
  data = JSON.stringify(data)
  this.receiveMessage = JSON.parse(data)
  cordova.fireDocumentEvent('jpush.receiveMessage', this.receiveMessage)
}

JPushPlugin.prototype.openNotificationInAndroidCallback = function (data) {
  data = JSON.stringify(data)
  this.openNotification = JSON.parse(data)
  cordova.fireDocumentEvent('jpush.openNotification', this.openNotification)
}

JPushPlugin.prototype.receiveNotificationInAndroidCallback = function (data) {
  data = JSON.stringify(data)
  this.receiveNotification = JSON.parse(data)
  cordova.fireDocumentEvent('jpush.receiveNotification', this.receiveNotification)
}

JPushPlugin.prototype.clearAllNotification = function () {
  if (device.platform === 'Android') {
    this.callNative('clearAllNotification', [], null)
  }
}

JPushPlugin.prototype.clearNotificationById = function (id) {
  if (device.platform === 'Android') {
    this.callNative('clearNotificationById', [id], null)
  }
}

JPushPlugin.prototype.setLatestNotificationNum = function (num) {
  if (device.platform === 'Android') {
    this.callNative('setLatestNotificationNum', [num], null)
  }
}

JPushPlugin.prototype.addLocalNotification = function (builderId, content, title,
  notificationID, broadcastTime, extras) {
  if (device.platform === 'Android') {
    this.callNative('addLocalNotification',
      [builderId, content, title, notificationID, broadcastTime, extras], null)
  }
}

JPushPlugin.prototype.removeLocalNotification = function (notificationID) {
  if (device.platform === 'Android') {
    this.callNative('removeLocalNotification', [notificationID], null)
  }
}

JPushPlugin.prototype.reportNotificationOpened = function (msgID) {
  if (device.platform === 'Android') {
    this.callNative('reportNotificationOpened', [msgID], null)
  }
}

/**
 * 用于在 Android 6.0 及以上系统,申请一些权限
 * 具体可看:http://docs.jpush.io/client/android_api/#android-60
 */
JPushPlugin.prototype.requestPermission = function () {
  if (device.platform === 'Android') {
    this.callNative('requestPermission', [], null)
  }
}

JPushPlugin.prototype.setSilenceTime = function (startHour, startMinute, endHour, endMinute) {
  if (device.platform === 'Android') {
    this.callNative('setSilenceTime', [startHour, startMinute, endHour, endMinute], null)
  }
}

JPushPlugin.prototype.setPushTime = function (weekdays, startHour, endHour) {
  if (device.platform === 'Android') {
    this.callNative('setPushTime', [weekdays, startHour, endHour], null)
  }
}

if (!window.plugins) {
  window.plugins = {}
}

if (!window.plugins.jPushPlugin) {
  window.plugins.jPushPlugin = new JPushPlugin()
}

module.exports = new JPushPlugin()