jpush.js 3.77 KB
import { updateJpush } from '@/api/jpush'
import { platform } from '@/config/index'
import { ref, reactive, onMounted } from 'vue';
export function useJPush() {
  //饮用JPush
  const JPushModule = uni.requireNativePlugin('JG-JPush')
  console.log('jpushModule', JPushModule)
  const isRegistrationPushed = ref(false)
  const registerID = ref(null)

  const eventRegister = () => {
    if(JPushModule) {
      JPushModule.addNotificationListener(result => {
	      console.log('通知事件回调:', result)
	      
	      // 判断事件类型
	      if (result.notificationEventType === 'notificationOpened') {
	        // 用户点击了通知
	        console.log('用户点击了通知')
	        console.log('通知内容:', result)
	        
	        // 获取自定义数据
	        if (result.extras) {
	          console.log('自定义数据:', result.extras)
	          // 根据自定义数据跳转页面
	          handleNotificationClick(result.extras)
	        }
	      } else if (result.notificationEventType === 'notificationArrived') {
	        // 通知到达
	        console.log('收到新通知')
	      }
	    })
	  
	    // 监听自定义消息(透传消息)
	    JPushModule.addCustomMessageListener(result => {
	      console.log('收到自定义消息:', result)
	    })
	    
	    // 监听本地通知点击
	    JPushModule.addLocalNotificationListener(result => {
	      console.log('本地通知被点击:', result)
	    })
    }
  }


  const startUploadJpush = () => {
    if (registerID.value) {
      console.log(`推送注册成功222222${registerID.value}`)
      isRegistrationPushed.value = true
	  
	    // JPushModule.initJPushService()
	    // 监听通知点击事件 - 这是关键
      eventRegister()
      
      updateJpush({ registrationId: registerID.value, platform: platform() })
        .then(() => {
          console.warn('推送注册成功')
          // tip.showToast(`推送注册成功${registerID.value}`)
          isRegistrationPushed.value = true
        })
        .catch((err) => {

          console.error('调用推送接口失败', err)
          // tip.showToast(`推送注册失败:${err}`)
        })
    }
  }

  const registerPush = () => {
    if (isRegistrationPushed.value) {
      return
    }
    if (registerID.value) {
	
      // 调用接口后台关联
      startUploadJpush()
      return
    }
    try {
      JPushModule.getRegistrationID((result) => {
        registerID.value = result.registerID
        console.warn('推送注册结果:', result)
        // tip.showToast(`推送注册结果:${registerID.value}`)
        startUploadJpush()
      })
    }
    catch (err) {
      console.error('推送注册异常', err)
      // tip.showToast(`推送注册异常 :${err}`)
    }
    // 可能registerPush异常失败,再重试一次
    setTimeout(() => {
      registerPush()
    }, 5000)
  }

  const checkNotificationAuthorization = () => {
    JPushModule.isNotificationEnabled((result) => {
      const code = result.code
      if (code !== 1) {
        uni.showToast({
          icon: 'none',
          title: '还没有通知权限,请在系统设置中开启',
          duration: 3000,
        })
        JPushModule.openSettingsForNotification((result) => {
          console.warn('打开通知设置结果:', result)
          if (result.code === 1) {
            // 用户已授权
            registerPush()
          }
          else {
            // 用户未授权
            uni.showToast({
              icon: 'none',
              title: '您还没有打开通知权限',
              duration: 3000,
            })
          }
        })
      }
    })
    JPushModule.requestPermission()
  }

  return {
    JPushModule,
    isRegistrationPushed,
    registerID,
    registerPush,
    checkNotificationAuthorization,
  }
}