uniapp对接极光推送(真是手把手的教了)
效果

第一步
在插件市场下载极光推送插件(共两个插件)
下载两个插件的流程一样,输入好包名,选择好要下载的项目,(两个插件下载流程一样)
下载链接
极光推送 JPush 官方 SDK - DCloud 插件市场
极光推送 JCore 官方 SDK - DCloud 插件市场



第二步
找到manifest.json文件点击app原生插件配置,点击选择云端插件,勾选上刚刚下载的两个插件,点击确定

再输入极光推送申请的key

在视图源码里在value填入极光推送的key

将下面的代码复制到App.vue文件里,跟onShow同级
引入import routingIntercept from '@/utils/permission.js';
onLaunch() {
// cache.clear();
// cache.set('token', 'ZTE3OGUyN2FhZmZhNDMwNmIzYjUwZDQyZDM2YTA1ZjcxNzA0ODg2NDQ2NTA3Mw==');
let userInfo = cache.get('userInfo');
console.log(this.userInfo);
if (!this.userInfo.nickName) {
this.updateUserInfo(userInfo);
}
// routingIntercept();
// uni.setStorageSync('token', 'OGMzZjdiYzE5OGQ2NDE5M2JhZjZmOWEyODAxMWRlMTUxNzAxMzk0NjMzMTU5MQ==');
console.log('App Launch')
const that = this
let platform = uni.getSystemInfoSync().platform
// #ifdef APP-PLUS
console.log('App Launch platform', platform)
const jpushModule = uni.requireNativePlugin("JG-JPush")
jpushModule.setLoggerEnable(true);
jpushModule.initJPushService();
// 监听是否连接成功
jpushModule.addConnectEventListener(result => {
let connectEnable = result.connectEnable;
console.log('addConnectEventListener', connectEnable)
// 触发在login.vue 中对连接状态的监听
uni.$emit('connectStatusChange', connectEnable)
});
jpushModule.addNotificationListener(result => {
console.log('result', result)
if (result.notificationEventType == "notificationOpened") {
plus.runtime.setBadgeNumber(0); // 清除应用图标上的角标
jpushModule.setBadge(0)
console.log('addNotificationListener')
//点击窗口通知栏推送的消息 跳转指定页面
uni.switchTab({
url: '/pages/order/pages/detail/detail?id=' + result.extras.businessId,
complete(res) {}
})
}
});
// #endif
uni.getSystemInfo({
success: (e) => {
let statusBar = 0 //状态栏高度
let customBar = 0 // 状态栏高度 + 导航栏高度
statusBar = e.statusBarHeight
customBar = e.statusBarHeight + 45
that.$store.commit('setStatusBarHeight', statusBar + 'px')
getApp().globalData.statusBarHeight = statusBar + 'px'
}
})
// if (that.$getCache('token')) {
// const {
// rows
// } = await that.$get('/system/dict/data/moveList', {
// pageNum: 1,
// pageSize: 9999
// })
// that.$setCache('dict', JSON.stringify(rows))
// }
// cache.clear();
// let token = uni.getStorageSync('token');
// if (!token) {
// uni.reLaunch({
// url: '/pages/operate/pages/login/login',
// });
// }
routingIntercept();
// uni.setStorageSync('token', 'OGMzZjdiYzE5OGQ2NDE5M2JhZjZmOWEyODAxMWRlMTUxNzAxMzk0NjMzMTU5MQ==');
},

上面代码中console.log('result', result)为推送回来的信息
到这一步重新打一个自定义基座,就可以了
**重新打一个自定义基座 重新打一个自定义基座 重新打一个自定义基座 重要的事情说三遍 **
然后在极光上就可以推送了
下面的代码可有可无,意思就是大概,获取到用户信息取个别名给了极光
getUserInfo: function() {
console.log(this)
this.$get("jkshopcloud-user/ma/user_detail_info", {}, true)
.then((res) => {
console.log(res.data);
this.userInfo = res.data || {}
let shopId = "0_" + res.data.userId
const jpush = uni.requireNativePlugin("JG-JPush")
console.log("shopId", shopId);
console.log("jpush", jpush);
jpush.setAlias({
sequence: 0,
alias: shopId
}, (result) => {
});
// uni.setStorageSync('userinfo', JSON.stringify(res.data));
// caches.set('shopStatus', res.data.applyStatus);
})
.finally(() => {});
},
此教程只能在应用的时候能推送,如果没有app则推送不过来,只有打开app所有的推送才会出来,如果想不app也能推送,可以考虑接入系统厂商通道
