uniapp接入极光推送

  • Post author:
  • Post category:uniapp


1.接入极光插件JCore官方SDK、JPsh官方SDK

2.在极光推送后台填入各厂家信息,在项目组填入对应信息。注:注意写入格式,需要跟示例一样!

3.创建极光推送js文件,并在main.js文件引入全局注册,在app.vue文件里判断token是否存在,存在则调用uni.JPushInit方法初始化极光推送,并且在链接成功后会调用JPushSetAlias方法设置别名,后面发送消息即可调用后端接口通过别名发送推送。

const jpushModule = uni.requireNativePlugin('JG-JPush')

function JPushInit() {
    if (uni.getSystemInfoSync().platform == "ios") {
        jpushModule.requestNotificationAuthorization((result) => {
            let status = result.status
            if (status < 2) {
                uni.showToast({
                    icon: 'none',
                    title: '您还没有打开通知权限',
                    duration: 3000
                })
            }
        })
    }
    jpushModule.setLoggerEnable(true);
    jpushModule.initJPushService();
    jpushModule.addConnectEventListener(result => {
        let connectEnable = result.connectEnable
        uni.$emit('connectStatusChange', connectEnable)
        if (connectEnable) {
            JPushSetAlias()
        }
    });
    jpushModule.addNotificationListener(result => {
        if (result.notificationEventType == "notificationOpened") {
            try {
                const notificationExtras = JSON.parse(result?.extras['androidNotification extras key'])
                const changeID =
                    `${notificationExtras?.conversationType}${notificationExtras?.conversationType=="GROUP"?notificationExtras?.to:notificationExtras?.from}`
                setTimeout(() => {
                    uni.store.commit('chat/changeID', changeID)
                    uni.navigateTo({
                        url: `/pages/TUI-Chat/chat`
                    });
                }, 1000)
            } catch (err) {
                uni.redirectTo({
                    url: "/pages/tabbar/tabbar"
                })
            }
        }

    });
    jpushModule?.setIsAllowedInMessagePop && jpushModule?.setIsAllowedInMessagePop(true)
}
uni['JPushInit'] = JPushInit
/**
 * 连接状态
 */
function JPushconnectStatus() {
    uni.$on('connectStatusChange', (connectStatus) => {
        let connectStr = ''
        if (connectStatus == true) {
            connectStr = '已连接'
            JPushSetRegistrationID()
        } else {
            connectStr = '未连接'
        }
        console.log('监听到了连接状态变化 --- ', connectStr)
    })
}
uni['JPushconnectStatus'] = JPushconnectStatus
/**
 * 获取推送ID
 */
function JPushSetRegistrationID() {
    jpushModule.getRegistrationID(result => {
        let registerID = result.registerID
    })
}
uni['JPushSetRegistrationID'] = JPushSetRegistrationID
/**
 * 设置别名
 */
function JPushSetAlias() {
    const userCode = uni.store?.state?.base?.user?.userCode
    userCode && jpushModule.setAlias({
        alias: _replaceAll(userCode, '-', '_'),
        sequence: Date.now()
    })
}
uni['JPushSetAlias'] = JPushSetAlias

function _replaceAll(str, str1, str2) {
    const arr = str?.split(str1)
    return arr?.join(str2)
}



版权声明:本文为weixin_49682182原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。