jpush.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // 引入极光推送插件
  2. //#ifdef APP-PLUS
  3. var jpushModule = uni.requireNativePlugin("JG-JPush");
  4. //#endif
  5. //#ifdef H5
  6. let jpushModule = 'h5端不知道为什么也会加载这玩意,不定义下面还会出错'
  7. //#endif
  8. export function appInit(phone) {
  9. // 如果未能加载,不执行后续
  10. if (!jpushModule) return
  11. // 开启debug模式
  12. jpushModule.setLoggerEnable(true);
  13. // 初始化SDK
  14. jpushModule.initJPushService();
  15. // 连接状态回调
  16. jpushModule.addConnectEventListener(result => {
  17. let connectEnable = result.connectEnable
  18. // true已连接, false未连接
  19. console.log("jpush连接", connectEnable)
  20. })
  21. // 通知数组
  22. let messageIDList = []
  23. // 通知事件回调
  24. jpushModule.addNotificationListener(result => {
  25. // 通过 notificationEventType字段区分是收到通知还是点击通知
  26. const {
  27. notificationEventType,
  28. messageID,
  29. title,
  30. content
  31. } = result
  32. messageIDList.push(messageID)
  33. if (notificationEventType == 'notificationOpened') {
  34. // 点击通知操作
  35. console.log('点击通知')
  36. } else if (notificationEventType == 'notificationArrived') {
  37. // 收到通知
  38. console.log('收到通知')
  39. }
  40. })
  41. // 获取应用程序的 RegistrationID。 只有当应用程序成功注册到 JPush 的服务器时才返回对应的值,否则返回空字符串
  42. jpushModule.getRegistrationID(result => {
  43. // RegistrationID单独发送消息用
  44. console.log(result);
  45. if (result.registerID) {
  46. console.log(result.registerID)
  47. uni.setStorageSync("register_id", result.registerID)
  48. }
  49. })
  50. }
  51. export function addUser(phone) {
  52. if (!jpushModule) return
  53. // 设置别名
  54. jpushModule.setAlias({
  55. "alias": phone,
  56. "sequence": 1
  57. })
  58. }
  59. export function delMsg() {
  60. if (!jpushModule) return
  61. // 设置别名
  62. // jpushModule.clearLocalNotifications()
  63. jpushModule.setBadge(0)
  64. }
  65. // 删除别名
  66. export function deleteAlias() {
  67. if (!jpushModule) return
  68. jpushModule.deleteAlias({
  69. 'sequence': 1
  70. })
  71. }