JPushPlugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. var JPushPlugin = function () {}
  2. // private plugin function
  3. JPushPlugin.prototype.receiveMessage = {}
  4. JPushPlugin.prototype.openNotification = {}
  5. JPushPlugin.prototype.receiveNotification = {}
  6. JPushPlugin.prototype.isPlatformIOS = function () {
  7. return (device.platform === 'iPhone' ||
  8. device.platform === 'iPad' ||
  9. device.platform === 'iPod touch' ||
  10. device.platform === 'iOS')
  11. }
  12. JPushPlugin.prototype.errorCallback = function (msg) {
  13. console.log('JPush Callback Error: ' + msg)
  14. }
  15. JPushPlugin.prototype.callNative = function (name, args, successCallback, errorCallback) {
  16. if (errorCallback) {
  17. cordova.exec(successCallback, errorCallback, 'JPushPlugin', name, args)
  18. } else {
  19. cordova.exec(successCallback, this.errorCallback, 'JPushPlugin', name, args)
  20. }
  21. }
  22. // Common methods
  23. JPushPlugin.prototype.init = function () {
  24. if (this.isPlatformIOS()) {
  25. this.callNative('initial', [], null)
  26. } else {
  27. this.callNative('init', [], null)
  28. }
  29. }
  30. JPushPlugin.prototype.setDebugMode = function (mode) {
  31. if (device.platform === 'Android') {
  32. this.callNative('setDebugMode', [mode], null)
  33. } else {
  34. if (mode === true) {
  35. this.setDebugModeFromIos()
  36. } else {
  37. this.setLogOFF()
  38. }
  39. }
  40. }
  41. JPushPlugin.prototype.getRegistrationID = function (successCallback) {
  42. this.callNative('getRegistrationID', [], successCallback)
  43. }
  44. JPushPlugin.prototype.stopPush = function () {
  45. this.callNative('stopPush', [], null)
  46. }
  47. JPushPlugin.prototype.resumePush = function () {
  48. this.callNative('resumePush', [], null)
  49. }
  50. JPushPlugin.prototype.isPushStopped = function (successCallback) {
  51. this.callNative('isPushStopped', [], successCallback)
  52. }
  53. JPushPlugin.prototype.clearLocalNotifications = function () {
  54. if (device.platform === 'Android') {
  55. this.callNative('clearLocalNotifications', [], null)
  56. } else {
  57. this.clearAllLocalNotifications()
  58. }
  59. }
  60. /**
  61. * 设置标签。
  62. * 注意:该接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
  63. *
  64. * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
  65. */
  66. JPushPlugin.prototype.setTags = function (params, successCallback, errorCallback) {
  67. this.callNative('setTags', [params], successCallback, errorCallback)
  68. }
  69. /**
  70. * 新增标签。
  71. *
  72. * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
  73. */
  74. JPushPlugin.prototype.addTags = function (params, successCallback, errorCallback) {
  75. this.callNative('addTags', [params], successCallback, errorCallback)
  76. }
  77. /**
  78. * 删除指定标签。
  79. *
  80. * @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
  81. */
  82. JPushPlugin.prototype.deleteTags = function (params, successCallback, errorCallback) {
  83. this.callNative('deleteTags', [params], successCallback, errorCallback)
  84. }
  85. /**
  86. * 清除所有标签。
  87. *
  88. * @param params = { 'sequence': number }
  89. */
  90. JPushPlugin.prototype.cleanTags = function (params, successCallback, errorCallback) {
  91. this.callNative('cleanTags', [params], successCallback, errorCallback)
  92. }
  93. /**
  94. * 查询所有标签。
  95. *
  96. * @param params = { 'sequence': number }
  97. */
  98. JPushPlugin.prototype.getAllTags = function (params, successCallback, errorCallback) {
  99. this.callNative('getAllTags', [params], successCallback, errorCallback)
  100. }
  101. /**
  102. * 查询指定标签与当前用户的绑定状态。
  103. *
  104. * @param params = { 'sequence': number, 'tag': string }
  105. */
  106. JPushPlugin.prototype.checkTagBindState = function (params, successCallback, errorCallback) {
  107. this.callNative('checkTagBindState', [params], successCallback, errorCallback)
  108. }
  109. /**
  110. * 设置别名。
  111. * 注意:该接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
  112. *
  113. * @param params = { 'sequence': number, 'alias': string }
  114. */
  115. JPushPlugin.prototype.setAlias = function (params, successCallback, errorCallback) {
  116. this.callNative('setAlias', [params], successCallback, errorCallback)
  117. }
  118. /**
  119. * 删除别名。
  120. *
  121. * @param params = { 'sequence': number }
  122. */
  123. JPushPlugin.prototype.deleteAlias = function (params, successCallback, errorCallback) {
  124. this.callNative('deleteAlias', [params], successCallback, errorCallback)
  125. }
  126. /**
  127. * 查询当前绑定的别名。
  128. *
  129. * @param params = { 'sequence': number }
  130. */
  131. JPushPlugin.prototype.getAlias = function (params, successCallback, errorCallback) {
  132. this.callNative('getAlias', [params], successCallback, errorCallback)
  133. }
  134. // 判断系统设置中是否对本应用启用通知。
  135. // iOS: 返回值如果大于 0,代表通知开启;0: 通知关闭。
  136. // UIRemoteNotificationTypeNone = 0,
  137. // UIRemoteNotificationTypeBadge = 1 << 0,
  138. // UIRemoteNotificationTypeSound = 1 << 1,
  139. // UIRemoteNotificationTypeAlert = 1 << 2,
  140. // UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3,
  141. // Android: 返回值 1 代表通知启用;0: 通知关闭。
  142. JPushPlugin.prototype.getUserNotificationSettings = function (successCallback) {
  143. if (this.isPlatformIOS()) {
  144. this.callNative('getUserNotificationSettings', [], successCallback)
  145. } else if (device.platform === 'Android') {
  146. this.callNative('areNotificationEnabled', [], successCallback)
  147. }
  148. }
  149. // iOS methods
  150. JPushPlugin.prototype.startJPushSDK = function () {
  151. this.callNative('startJPushSDK', [], null)
  152. }
  153. JPushPlugin.prototype.setBadge = function (value) {
  154. if (this.isPlatformIOS()) {
  155. this.callNative('setBadge', [value], null)
  156. }
  157. }
  158. JPushPlugin.prototype.resetBadge = function () {
  159. if (this.isPlatformIOS()) {
  160. this.callNative('resetBadge', [], null)
  161. }
  162. }
  163. JPushPlugin.prototype.setDebugModeFromIos = function () {
  164. if (this.isPlatformIOS()) {
  165. this.callNative('setDebugModeFromIos', [], null)
  166. }
  167. }
  168. JPushPlugin.prototype.setLogOFF = function () {
  169. if (this.isPlatformIOS()) {
  170. this.callNative('setLogOFF', [], null)
  171. }
  172. }
  173. JPushPlugin.prototype.setCrashLogON = function () {
  174. if (this.isPlatformIOS()) {
  175. this.callNative('crashLogON', [], null)
  176. }
  177. }
  178. JPushPlugin.prototype.addLocalNotificationForIOS = function (delayTime, content,
  179. badge, notificationID, extras) {
  180. if (this.isPlatformIOS()) {
  181. this.callNative('setLocalNotification', [delayTime, content, badge, notificationID, extras], null)
  182. }
  183. }
  184. JPushPlugin.prototype.deleteLocalNotificationWithIdentifierKeyInIOS = function (identifierKey) {
  185. if (this.isPlatformIOS()) {
  186. this.callNative('deleteLocalNotificationWithIdentifierKey', [identifierKey], null)
  187. }
  188. }
  189. JPushPlugin.prototype.clearAllLocalNotifications = function () {
  190. if (this.isPlatformIOS()) {
  191. this.callNative('clearAllLocalNotifications', [], null)
  192. }
  193. }
  194. JPushPlugin.prototype.setLocation = function (latitude, longitude) {
  195. if (this.isPlatformIOS()) {
  196. this.callNative('setLocation', [latitude, longitude], null)
  197. }
  198. }
  199. JPushPlugin.prototype.startLogPageView = function (pageName) {
  200. if (this.isPlatformIOS()) {
  201. this.callNative('startLogPageView', [pageName], null)
  202. }
  203. }
  204. JPushPlugin.prototype.stopLogPageView = function (pageName) {
  205. if (this.isPlatformIOS()) {
  206. this.callNative('stopLogPageView', [pageName], null)
  207. }
  208. }
  209. JPushPlugin.prototype.beginLogPageView = function (pageName, duration) {
  210. if (this.isPlatformIOS()) {
  211. this.callNative('beginLogPageView', [pageName, duration], null)
  212. }
  213. }
  214. JPushPlugin.prototype.setApplicationIconBadgeNumber = function (badge) {
  215. if (this.isPlatformIOS()) {
  216. this.callNative('setApplicationIconBadgeNumber', [badge], null)
  217. }
  218. }
  219. JPushPlugin.prototype.getApplicationIconBadgeNumber = function (callback) {
  220. if (this.isPlatformIOS()) {
  221. this.callNative('getApplicationIconBadgeNumber', [], callback)
  222. }
  223. }
  224. JPushPlugin.prototype.addDismissActions = function (actions, categoryId) {
  225. this.callNative('addDismissActions', [actions, categoryId])
  226. }
  227. JPushPlugin.prototype.addNotificationActions = function (actions, categoryId) {
  228. this.callNative('addNotificationActions', [actions, categoryId])
  229. }
  230. // Android methods
  231. JPushPlugin.prototype.getConnectionState = function (successCallback) {
  232. if (device.platform === 'Android') {
  233. this.callNative('getConnectionState', [], successCallback)
  234. }
  235. }
  236. JPushPlugin.prototype.setBasicPushNotificationBuilder = function () {
  237. if (device.platform === 'Android') {
  238. this.callNative('setBasicPushNotificationBuilder', [], null)
  239. }
  240. }
  241. JPushPlugin.prototype.setCustomPushNotificationBuilder = function () {
  242. if (device.platform === 'Android') {
  243. this.callNative('setCustomPushNotificationBuilder', [], null)
  244. }
  245. }
  246. JPushPlugin.prototype.receiveRegistrationIdInAndroidCallback = function (data) {
  247. if (device.platform === 'Android') {
  248. data = JSON.stringify(data)
  249. var event = JSON.parse(data)
  250. cordova.fireDocumentEvent('jpush.receiveRegistrationId', event)
  251. }
  252. }
  253. JPushPlugin.prototype.receiveMessageInAndroidCallback = function (data) {
  254. data = JSON.stringify(data)
  255. this.receiveMessage = JSON.parse(data)
  256. cordova.fireDocumentEvent('jpush.receiveMessage', this.receiveMessage)
  257. }
  258. JPushPlugin.prototype.openNotificationInAndroidCallback = function (data) {
  259. data = JSON.stringify(data)
  260. this.openNotification = JSON.parse(data)
  261. cordova.fireDocumentEvent('jpush.openNotification', this.openNotification)
  262. }
  263. JPushPlugin.prototype.receiveNotificationInAndroidCallback = function (data) {
  264. data = JSON.stringify(data)
  265. this.receiveNotification = JSON.parse(data)
  266. cordova.fireDocumentEvent('jpush.receiveNotification', this.receiveNotification)
  267. }
  268. JPushPlugin.prototype.clearAllNotification = function () {
  269. if (device.platform === 'Android') {
  270. this.callNative('clearAllNotification', [], null)
  271. }
  272. }
  273. JPushPlugin.prototype.clearNotificationById = function (id) {
  274. if (device.platform === 'Android') {
  275. this.callNative('clearNotificationById', [id], null)
  276. }
  277. }
  278. JPushPlugin.prototype.setLatestNotificationNum = function (num) {
  279. if (device.platform === 'Android') {
  280. this.callNative('setLatestNotificationNum', [num], null)
  281. }
  282. }
  283. JPushPlugin.prototype.addLocalNotification = function (builderId, content, title,
  284. notificationID, broadcastTime, extras) {
  285. if (device.platform === 'Android') {
  286. this.callNative('addLocalNotification',
  287. [builderId, content, title, notificationID, broadcastTime, extras], null)
  288. }
  289. }
  290. JPushPlugin.prototype.removeLocalNotification = function (notificationID) {
  291. if (device.platform === 'Android') {
  292. this.callNative('removeLocalNotification', [notificationID], null)
  293. }
  294. }
  295. JPushPlugin.prototype.reportNotificationOpened = function (msgID) {
  296. if (device.platform === 'Android') {
  297. this.callNative('reportNotificationOpened', [msgID], null)
  298. }
  299. }
  300. /**
  301. * 用于在 Android 6.0 及以上系统,申请一些权限
  302. * 具体可看:http://docs.jpush.io/client/android_api/#android-60
  303. */
  304. JPushPlugin.prototype.requestPermission = function () {
  305. if (device.platform === 'Android') {
  306. this.callNative('requestPermission', [], null)
  307. }
  308. }
  309. JPushPlugin.prototype.setSilenceTime = function (startHour, startMinute, endHour, endMinute) {
  310. if (device.platform === 'Android') {
  311. this.callNative('setSilenceTime', [startHour, startMinute, endHour, endMinute], null)
  312. }
  313. }
  314. JPushPlugin.prototype.setPushTime = function (weekdays, startHour, endHour) {
  315. if (device.platform === 'Android') {
  316. this.callNative('setPushTime', [weekdays, startHour, endHour], null)
  317. }
  318. }
  319. if (!window.plugins) {
  320. window.plugins = {}
  321. }
  322. if (!window.plugins.jPushPlugin) {
  323. window.plugins.jPushPlugin = new JPushPlugin()
  324. }
  325. module.exports = new JPushPlugin()