JPushPlugin.js 12 KB

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