1.极光配置
https://www.jiguang.cn/console/#/home
1.1创建应用
1.2服务集成(勾选消息推送)
1.3包名配置(安卓直接填写,ios上传证书自动识别(开发证书和生产证书),一定要填写正确,否则无法推送,无法修改,只能删除重新创建应用)配置完成第一步就算完成了
2.购买极光推送插件(包名为1.3步配置的包名,填错了的话需要重新购买,一般会买两次,第一次安卓和ios开发证书包名,第二次ios生成证书包名,楼主就是这样多花了9.9))
JYJPush谷歌版极光推送插件,支持GooglePlay - DCloud 插件市场
3.uniapp manifest.json文件配置
4.使用
4.1App.vue设置别名
想了解更多插件市场写的很详细,上方有链接
<script>
//极光推送
// #ifdef APP-PLUS || APP
const jyJPush = uni.requireNativePlugin('JY-JPushGoogle');
// const innerAudioContext = uni.getBackgroundAudioManager();
// #endif
export default {
onLaunch() {
// #ifdef APP-PLUS || APP
this.initJyJPush()
this.watchPush()
this.watchPushClick()
// #endif
},
onShow() {
// #ifdef APP-PLUS || APP
// if (uni.getSystemInfoSync().platform == "ios") {
// var GtSdk = plus.ios.importClass("GeTuiSdk");
// GtSdk.setBadge(0);
// console.log('清除角标')
plus.runtime.setBadgeNumber(0);
// }else{
// }
// #endif
},
methods: {
//监听消息推送事件
// #ifdef APP-PLUS || APP
watchPush() {
jyJPush.addJYJPushReceiveNotificationListener(result => {
// 监听成功后,若收到推送,会在result返回对应的数据;数据格式保持极光返回的安卓/iOS数据一致
// innerAudioContext.src = 'https://blfcq-1305180334.cos.ap-chongqing.myqcloud.com/assets/order-hint.mp3';
// innerAudioContext.play()
// innerAudioContext.onPlay(() => {
// console.log('开始播放');
// });
console.log(' watchPush result', result);
// uni.showToast({
// icon: 'none',
// title: JSON.stringify(result)
// })
});
},
//监听推送点击事件
watchPushClick() {
jyJPush.addJYJPushReceiveOpenNotificationListener(result => {
// 监听成功后,若点击推送消息,会触发result;数据格式保持极光返回的安卓/iOS数据一致
console.log(' ClickPush result', result);
// uni.showToast({
// icon: 'none',
// title: JSON.stringify(result)
// })
if (this.vuex_user && this.vuex_user.id && this.vuex_token) {
uni.navigateTo({
url: '/pages/newPages/mall/mines/message'
})
} else {
this.$u.route({
type: 'redirect',
url: 'pages/account/login'
})
}
});
},
initJyJPush() {
jyJPush.android_init(res => {
//初始化成功成功之后再设置别名
//官网的话:init后直接set操作有极大可能导致失败,可能会在回调里拿到6022,6002等,测试的时候可以做个7、8秒的延时,正式业务里一般配合用户注册使用,延时基本上够用
//实际上:我设置成10s才管用
setTimeout(() => {
console.log('setAlias');
this.setAlias()
}, 1000 * 10) //20秒后再设置别名
console.log('init push res', res);
// uni.showToast({
// icon: 'none',
// title: JSON.stringify(res)
// })
})
},
//设置别名
setAlias() {
jyJPush.deleteJYJPushAlias({}, res => {
// console.log('角标删除1')
jyJPush.setJYJPushAlias({
userAlias: uni.getStorageSync('lifeData').vuex_user.code
},
result => {
console.log('setJYJPushAlias', JSON.stringify(result));
// uni.showToast({
// icon: 'none',
// title: JSON.stringify(result)
// })
});
})
},
// #endif
</script>
4.2前端在需要的地方调用后端的接口,后端将信息推送到该别名下的设备即可收到推送(后端如何实现楼主也不知道,问后端去,哈哈)