关于uniapp:uniapp集成极光推送

8次阅读

共计 1463 个字符,预计需要花费 4 分钟才能阅读完成。

本文旨在记录应用极光推送中遇到的坑

注册极光账号并获取 AppKey

去极光官网注册账号,并创立利用,填写包名,获取 AppKey。

引入极光推送插件

极光推送依赖 极光 JCore 官网 SDK,所以首先须要引入 极光 JCore 官网 SDK。而后引入 极光 JPush 官网 SDK。而后在我的项目的 manifest.json 中,找到 App 原生插件配置
抉择咱们援用的 云端插件

填写获取到的AppKey

援用曾经实现,上面开始编码。

调用极光推送

common 中新建jpush.js

  • 公共 API
  • Android 独有 API
  • iOS 独有 API
// 引入极光推送插件
var jpushModule = uni.requireNativePlugin("JG-JPush");

export default () => {
    // 开启 debug 模式
    jpushModule.setLoggerEnable(true);
    // 初始化 SDK
    jpushModule.initJPushService();
    
    // 连贯状态回调
    jpushModule.addConnectEventListener(result => {
        let connectEnable = result.connectEnable
        // true 已连贯, false 未连贯
        console.log("jpush 连贯", connectEnable)
    })

    // 设置别名
    jpushModule.setAlias({
        "alias": "别名",
        "sequence": 1
    })
    
    // 告诉事件回调
    jpushModule.addNotificationListener(result => {
        // 通过 notificationEventType 字段辨别是收到告诉还是点击告诉
        const {notificationEventType, messageID, title, content} = result
        if(notificationEventType == 'notificationOpened') {// 点击告诉操作} else if(notificationEventType == 'notificationArrived') {// 收到告诉}
    })
    
    // 获取应用程序的 RegistrationID。只有当应用程序胜利注册到 JPush 的服务器时才返回对应的值,否则返回空字符串
    jpushModule.getRegistrationID(result => {if (result.registerID) {uni.setStorageSync("register_id", result.registerID)
        }
    })
    
    // 自定义音讯,不会显示在告诉栏里
    jpushModule.addCustomMessageListener(result => {console.log("自定义音讯", result)
    })
}

App.vue 中引入jpush.js

import jpush from '@/common/jpush.js'

export default {onLaunch() {jpush()
    }
}

Android 相干 API

监测推送状态

jpushModule.isPushStopped(res => {
    // code   0 已进行推送  1 未进行推送
    const {code} = res
})

敞开推送

jpushModule.stopPush();

复原推送

jpushModule.resumePush();

公共 API

移除角标(iOS 下测试失常,Android 没测试)

jpushModule.setBadge(0)
plus.runtime.setBadgeNumber(0)
正文完
 0