uniapp消息推送
uniapp消息推送
·
export default {
onLoad() {
},
onLaunch: function() {
var that = this
// #ifdef APP-PLUS
// const _handlePush = function(message) {
// uni.navigateTo({
// url: message.payload.url + '?id=' + message.payload.id
// });
// };
// plus.push.addEventListener('click', _handlePush);
// plus.push.addEventListener('receive', _handlePush);
// #endif
plus.push.addEventListener("click", function(msg) {
var platform = uni.getSystemInfoSync().platform;
if (platform == "ios") {
// var lop = JSON.parse(msg.payload)
var lop = msg.payload
if (lop.flag == false) {
uni.navigateTo({
url: lop.url + '?id=' + lop.id + '&flag=' + lop.flag
});
} else {
uni.navigateTo({
url: lop.url + '?id=' + lop.id
});
}
} else if (platform == 'android') {
if (msg.payload.flag == false) {
uni.navigateTo({
url: msg.payload.url + '?id=' + msg.payload.id + '&flag=' +
msg.payload.flag
});
} else {
uni.navigateTo({
url: msg.payload.url + '?id=' + msg.payload.id
});
}
}
let pinf = plus.push.getClientInfo();
}, false);
plus.push.addEventListener("receive", function(msg) {
let pinf = plus.push.getClientInfo();
var platform = uni.getSystemInfoSync().platform;
if (platform == "ios") {
//ios平台应用在前台时,不能收到通知消息,只能走透传,在创建一条本地消息
if (msg.type == "receive") { // 这里判断触发的来源,否则一直推送。
// var content = msg.content;
plus.push.createMessage(msg.content, msg.payload, {
title: msg.title
});
}
} else if (platform == 'android') {
let options = {
cover: false,
sound: 'system',
title: msg.title
};
plus.push.createMessage(msg.content, JSON.stringify(msg.payload), options);
}
}, false)
// #ifdef APP-PLUS
var that = this
plus.runtime.getProperty(plus.runtime.appid, async function(widgetInfo) {
// console.log('1122222',widgetInfo)
var platform = uni.getSystemInfoSync().platform;
uni.request({
url: 'http://121.43.176.190:9091/laboratory/version',
method: 'get', //请求方式
data: {
version: widgetInfo.version,
system: platform
},
dataType: 'json',
header: {},
success: res => {
// console.log(21212,res)
if (res.data.code == 200) {
if (widgetInfo.version < res.data.data.version) {
that.downWgt(res.data.data.url)
}
}
},
fail: error => {
console.log(error);
}
});
});
// #endif
},
methods: {
...mapMutations(['updatePushMessage']),
/** App资源在线升级更新 */
// 下载wgt文件
downWgt(wgtUrl) {
let that = this;
plus.nativeUI.showWaiting('下载更新文件...')
plus.downloader.createDownload(wgtUrl, {
filename: '_doc/update/'
}, function(d, status) {
if (status == 200) {
// console.log(88888,d.filename)
that.installWgt(d.filename)
} else {
plus.nativeUI.alert('下载更新文件失败!')
}
plus.nativeUI.closeWaiting()
}).start()
},
// 更新应用资源包(wgt文件)
installWgt(path) {
// console.log(222,path)
plus.nativeUI.showWaiting('正在安装更新文件...')
plus.runtime.install(path, {}, function() {
plus.nativeUI.closeWaiting()
plus.runtime.restart()
// console.log('安装更新文件成功!')
// plus.nativeUI.alert('应用资源更新完成!', function() {
// plus.runtime.restart()
// })
}, function(e) {
// console.log(7777777777,e)
plus.nativeUI.closeWaiting()
plus.nativeUI.alert('安装更新文件失败[' + e.code + ']:' + e.message)
})
},
},
// onLaunch: function() {
// console.log('App Launch')
// },
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
}
更多推荐
已为社区贡献1条内容
所有评论(0)