JPushPlugin.js 11 KB

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