uni-app ios 在线推送 和 store 配置
App.vue onLaunch中添加以下代码
const _self = this;
const _handlePush = function(message) {
/**
* 通过 vuex 来同步页面的数据,仅做演示。
* 实际开发中,这里可能是跳转到某个页面等操作,请根据自身业务需求编写。
*/
_self.updatePushMessage(message);
uni.navigateTo({
//点击后页面路径
})
};
var platform = uni.getSystemInfoSync().platform;
//#ifdef APP-PLUS
/* 5+ push 消息推送 ps:使用:H5+的方式监听,实现推送*/
plus.push.addEventListener("click", function(msgc) {
// && msgc.type == 'click'
if (platform == 'ios') {
//离线是payload为object , 在线时本地创建的为string(本地创建通知只能传string,否则无法传递payload)
if (typeof msgc.payload == 'string' && msgc.payload.indexOf('{') > -1) {
var paymsg = JSON.parse(msgc.payload);
} else {
console.log('obj')
var paymsg = msgc.payload;
}
console.log(paymsg)
//执行跳转判断
}
_handlePush(msgc)
//这里可以写跳转业务代码
}, false);
// 监听在线消息事件
plus.push.addEventListener("receive", function(msg) {
//这里可以写跳转业务代码
if (msg.type == "receive") // 这里判断触发的来源,否则一直推送。
{
if (platform == 'ios') {
console.log(msg.payload)
plus.push.createMessage(
msg.payload,
//本地创建通知payload只能传string,否则无法成功传递(为null)
JSON.stringify(msg.payload), {
cover: false,
title: msg.title,
});
}
}
if (platform == 'android') {
//执行跳转判断
}
}, false);
//#endif
store 设置
// #ifndef VUE3
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
// #endif
// #ifdef VUE3
import {
createStore
} from 'vuex'
const store = createStore({
// #endif
state: {
pushMessage: {}
},
getters: {
pushMessage: state => state.pushMessage,
},
mutations: {
updatePushMessage(state, message) {
/**
* 注意:这里为了方便预览查看效果,始终对 payload 做了序列化的处理。
* 实际开发期中,请自行调整代码并注意发送的 payload 消息格式。
*/
let payload = message.payload;
if (typeof payload !== 'string') {
message.payload = JSON.stringify(payload);
}
state.pushMessage = message || {};
}
}
})
export default store