----uniCloud-aliyun/cloudfunctions/uniPush/index.js-----
'use strict';
const uniPush = uniCloud.getPushManager({appId:"xxxxxxx"}) //注意这里需要传入你的应用appId
exports.main = async (event, context) => {
console.log('event',event)
const { body } = event || {}
console.log('body',body)
let obj = {}
if(typeof body == "string" && body){
obj = JSON.parse(body)
}else if(typeof body == 'object' && body){
obj = body
}
let { push_clientid,title,content,url,platform } = obj
console.log('sendParams',obj)
return await uniPush.sendMessage({
"push_clientid":push_clientid,
"title":title,
"content":content,
"platform":platform,
"force_notification":true,
"payload": {
"url":url
},
"badge":1
})
};
----App.vue----
onLaunch: function (options) {
// #ifdef APP-PLUS
addListenMessagePush() //监听推送消息
// #endif
},
/**
-
监听app消息推送处理
*/
const addListenMessagePush = () => {
try {
uni.onPushMessage((res) => {
console.log('AppPushRes', res)
const { payload: { url } } = res.data
console.log('url',url)
plus.runtime.setBadgeNumber(0)
if (url) {
setTimeout(() => {
navigateToH5(url)
}, 1000)
}
})} catch (e) {
}
}