JPushPlugin.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. JPushPlugin.prototype.setTagsWithAlias = function (tags, alias, successCallback, errorCallback) {
  61. if (tags == null) {
  62. this.setAlias(alias)
  63. return
  64. }
  65. if (alias == null) {
  66. this.setTags(tags)
  67. return
  68. }
  69. var arrayTagWithAlias = [tags]
  70. arrayTagWithAlias.unshift(alias)
  71. this.callNative('setTagsWithAlias', arrayTagWithAlias, successCallback, errorCallback)
  72. }
  73. JPushPlugin.prototype.setTags = function (tags, successCallback, errorCallback) {
  74. this.callNative('setTags', tags, successCallback, errorCallback)
  75. }
  76. JPushPlugin.prototype.setAlias = function (alias, successCallback, errorCallback) {
  77. this.callNative('setAlias', [alias], successCallback, errorCallback)
  78. }
  79. // 判断系统设置中是否对本应用启用通知。
  80. // iOS: 返回值如果大于 0,代表通知开启;0: 通知关闭。
  81. // UIRemoteNotificationTypeNone = 0,
  82. // UIRemoteNotificationTypeBadge = 1 << 0,
  83. // UIRemoteNotificationTypeSound = 1 << 1,
  84. // UIRemoteNotificationTypeAlert = 1 << 2,
  85. // UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3,
  86. // Android: 返回值 1 代表通知启用;0: 通知关闭。
  87. JPushPlugin.prototype.getUserNotificationSettings = function (successCallback) {
  88. if (this.isPlatformIOS()) {
  89. this.callNative('getUserNotificationSettings', [], successCallback)
  90. } else if (device.platform === 'Android') {
  91. this.callNative('areNotificationEnabled', [], successCallback)
  92. }
  93. }
  94. // iOS methods
  95. JPushPlugin.prototype.startJPushSDK = function () {
  96. this.callNative('startJPushSDK', [], null)
  97. }
  98. JPushPlugin.prototype.setBadge = function (value) {
  99. if (this.isPlatformIOS()) {
  100. this.callNative('setBadge', [value], null)
  101. }
  102. }
  103. JPushPlugin.prototype.resetBadge = function () {
  104. if (this.isPlatformIOS()) {
  105. this.callNative('resetBadge', [], null)
  106. }
  107. }
  108. JPushPlugin.prototype.setDebugModeFromIos = function () {
  109. if (this.isPlatformIOS()) {
  110. this.callNative('setDebugModeFromIos', [], null)
  111. }
  112. }
  113. JPushPlugin.prototype.setLogOFF = function () {
  114. if (this.isPlatformIOS()) {
  115. this.callNative('setLogOFF', [], null)
  116. }
  117. }
  118. JPushPlugin.prototype.setCrashLogON = function () {
  119. if (this.isPlatformIOS()) {
  120. this.callNative('crashLogON', [], null)
  121. }
  122. }
  123. JPushPlugin.prototype.addLocalNotificationForIOS = function (delayTime, content,
  124. badge, notificationID, extras) {
  125. if (this.isPlatformIOS()) {
  126. this.callNative('setLocalNotification', [delayTime, content, badge, notificationID, extras], null)
  127. }
  128. }
  129. JPushPlugin.prototype.deleteLocalNotificationWithIdentifierKeyInIOS = function (identifierKey) {
  130. if (this.isPlatformIOS()) {
  131. this.callNative('deleteLocalNotificationWithIdentifierKey', [identifierKey], null)
  132. }
  133. }
  134. JPushPlugin.prototype.clearAllLocalNotifications = function () {
  135. if (this.isPlatformIOS()) {
  136. this.callNative('clearAllLocalNotifications', [], null)
  137. }
  138. }
  139. JPushPlugin.prototype.setLocation = function (latitude, longitude) {
  140. if (this.isPlatformIOS()) {
  141. this.callNative('setLocation', [latitude, longitude], null)
  142. }
  143. }
  144. JPushPlugin.prototype.startLogPageView = function (pageName) {
  145. if (this.isPlatformIOS()) {
  146. this.callNative('startLogPageView', [pageName], null)
  147. }
  148. }
  149. JPushPlugin.prototype.stopLogPageView = function (pageName) {
  150. if (this.isPlatformIOS()) {
  151. this.callNative('stopLogPageView', [pageName], null)
  152. }
  153. }
  154. JPushPlugin.prototype.beginLogPageView = function (pageName, duration) {
  155. if (this.isPlatformIOS()) {
  156. this.callNative('beginLogPageView', [pageName, duration], null)
  157. }
  158. }
  159. JPushPlugin.prototype.setApplicationIconBadgeNumber = function (badge) {
  160. if (this.isPlatformIOS()) {
  161. this.callNative('setApplicationIconBadgeNumber', [badge], null)
  162. }
  163. }
  164. JPushPlugin.prototype.getApplicationIconBadgeNumber = function (callback) {
  165. if (this.isPlatformIOS()) {
  166. this.callNative('getApplicationIconBadgeNumber', [], callback)
  167. }
  168. }
  169. JPushPlugin.prototype.addDismissActions = function (actions, categoryId) {
  170. this.callNative('addDismissActions', [actions, categoryId])
  171. }
  172. JPushPlugin.prototype.addNotificationActions = function (actions, categoryId) {
  173. this.callNative('addNotificationActions', [actions, categoryId])
  174. }
  175. // Android methods
  176. JPushPlugin.prototype.getConnectionState = function (successCallback) {
  177. if (device.platform === 'Android') {
  178. this.callNative('getConnectionState', [], successCallback)
  179. }
  180. }
  181. JPushPlugin.prototype.setBasicPushNotificationBuilder = function () {
  182. if (device.platform === 'Android') {
  183. this.callNative('setBasicPushNotificationBuilder', [], null)
  184. }
  185. }
  186. JPushPlugin.prototype.setCustomPushNotificationBuilder = function () {
  187. if (device.platform === 'Android') {
  188. this.callNative('setCustomPushNotificationBuilder', [], null)
  189. }
  190. }
  191. JPushPlugin.prototype.receiveRegistrationIdInAndroidCallback = function (data) {
  192. if (device.platform === 'Android') {
  193. data = JSON.stringify(data)
  194. var event = JSON.parse(data)
  195. cordova.fireDocumentEvent('jpush.receiveRegistrationId', event)
  196. }
  197. }
  198. JPushPlugin.prototype.receiveMessageInAndroidCallback = function (data) {
  199. data = JSON.stringify(data)
  200. console.log('JPushPlugin:receiveMessageInAndroidCallback: ' + data)
  201. this.receiveMessage = JSON.parse(data)
  202. cordova.fireDocumentEvent('jpush.receiveMessage', this.receiveMessage)
  203. }
  204. JPushPlugin.prototype.openNotificationInAndroidCallback = function (data) {
  205. data = JSON.stringify(data)
  206. console.log('JPushPlugin:openNotificationInAndroidCallback: ' + data)
  207. this.openNotification = JSON.parse(data)
  208. cordova.fireDocumentEvent('jpush.openNotification', this.openNotification)
  209. }
  210. JPushPlugin.prototype.receiveNotificationInAndroidCallback = function (data) {
  211. data = JSON.stringify(data)
  212. console.log('JPushPlugin:receiveNotificationInAndroidCallback: ' + data)
  213. this.receiveNotification = JSON.parse(data)
  214. cordova.fireDocumentEvent('jpush.receiveNotification', this.receiveNotification)
  215. }
  216. JPushPlugin.prototype.clearAllNotification = function () {
  217. if (device.platform === 'Android') {
  218. this.callNative('clearAllNotification', [], null)
  219. }
  220. }
  221. JPushPlugin.prototype.clearNotificationById = function (id) {
  222. if (device.platform === 'Android') {
  223. this.callNative('clearNotificationById', [id], null)
  224. }
  225. }
  226. JPushPlugin.prototype.setLatestNotificationNum = function (num) {
  227. if (device.platform === 'Android') {
  228. this.callNative('setLatestNotificationNum', [num], null)
  229. }
  230. }
  231. JPushPlugin.prototype.addLocalNotification = function (builderId, content, title,
  232. notificationID, broadcastTime, extras) {
  233. if (device.platform === 'Android') {
  234. this.callNative('addLocalNotification',
  235. [builderId, content, title, notificationID, broadcastTime, extras], null)
  236. }
  237. }
  238. JPushPlugin.prototype.removeLocalNotification = function (notificationID) {
  239. if (device.platform === 'Android') {
  240. this.callNative('removeLocalNotification', [notificationID], null)
  241. }
  242. }
  243. JPushPlugin.prototype.reportNotificationOpened = function (msgID) {
  244. if (device.platform === 'Android') {
  245. this.callNative('reportNotificationOpened', [msgID], null)
  246. }
  247. }
  248. /**
  249. *是否开启统计分析功能,用于“用户使用时长”,“活跃用户”,“用户打开次数”的统计,并上报到服务器上,
  250. *在 Portal 上展示给开发者。
  251. */
  252. JPushPlugin.prototype.setStatisticsOpen = function (mode) {
  253. if (device.platform === 'Android') {
  254. this.callNative('setStatisticsOpen', [mode], null)
  255. }
  256. }
  257. /**
  258. * 用于在 Android 6.0 及以上系统,申请一些权限
  259. * 具体可看:http://docs.jpush.io/client/android_api/#android-60
  260. */
  261. JPushPlugin.prototype.requestPermission = function () {
  262. if (device.platform === 'Android') {
  263. this.callNative('requestPermission', [], null)
  264. }
  265. }
  266. JPushPlugin.prototype.setSilenceTime = function (startHour, startMinute, endHour, endMinute) {
  267. if (device.platform === 'Android') {
  268. this.callNative('setSilenceTime', [startHour, startMinute, endHour, endMinute], null)
  269. }
  270. }
  271. JPushPlugin.prototype.setPushTime = function (weekdays, startHour, endHour) {
  272. if (device.platform === 'Android') {
  273. this.callNative('setPushTime', [weekdays, startHour, endHour], null)
  274. }
  275. }
  276. if (!window.plugins) {
  277. window.plugins = {}
  278. }
  279. if (!window.plugins.jPushPlugin) {
  280. window.plugins.jPushPlugin = new JPushPlugin()
  281. }
  282. module.exports = new JPushPlugin()