服务的php代码
public static function sendAppMsg($cid,$title,$content,$payload)
{
$url = 'https://fc-mp-b5eb8***********com/pushmsg';
$request_id = md5(base64_encode($cid . $title . $content.time()));
$data = [
"cids" => [$cid],
"title"=>$title,
"content"=>$content,
"data"=>$payload,// 字符串
"request_id"=>$request_id,
"open_url"=>"",
];
$jsonData = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); // 使用 POST 请求
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($ch);
$result = json_decode($response,true);
var_dump($result);
curl_close($ch);
}
云函数代码
'use strict';
const uniPush = uniCloud.getPushManager({
appId: "__UNI__EFA6C11" // 你的应用appId
})
exports.main = async (event) => {
// event为客户端上传的参数
const body = JSON.parse(event.body)
console.log('参数',body)
// const body = event
return await uniPush.sendMessage({
"force_notification": false,// 填写true,客户端就会对在线消息自动创建“通知栏消息”,不填写则需要客户端自己处理。
"push_clientid": body.cids, // 必选 设备id,支持多个以数组的形式指定多个设备,如["cid-1","cid-2"],数组长度不大于1000
"title": body.title, //必填 通知栏显示的标题
"content": body.content, //必填 通知栏显示的内容
"payload": body.data, //可选 自定义数据
"open_url": body.open_url, //可选 需要打开外部url就填写
"request_id": body.request_id, //必填 请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
"badge":"+1",
// options更多参数介绍:https://doc.dcloud.net.cn/uniCloud/uni-cloud-push/options.html
"options": {
"HW": {
// 1 表示华为测试消息,华为每个应用每日可发送该测试消息500条,target_user_type 参数请勿发布至线上。
"/message/android/target_user_type": 1,
// "/message/android/category": "WORK"
},
"HO": {
//值为int 类型。1 表示测试推送,不填默认为0。荣耀每个应用每日可发送该测试消息1000条。此测试参数请勿发布至线上。
"/android/targetUserType": 1
},
"VV": {
//值为int 类型。0 表示正式推送;1 表示测试推送,不填默认为0。此 pushMode 参数请勿发布至线上。
"/pushMode": 1
},
"XM": {
//新小米消息分类下,私信公信id都必须要传,否则请求小米厂商接口会被拦截
"/extra.channel_id": "填写小米平台申请的渠道id"
}
}
})
};
app.vue代码
console.log('监听消息',msg)
})
plus.push.addEventListener("receive", function(msg) {
console.log('在线消息',msg)
});
console.log('App Launch')
uni.onPushMessage((res) => {
// 监听通知栏消息的点击
console.log("接收到消息",res)
if(res.type == 'click'){
// 如果需要跳转app内指定页面,则自己实现下方的跳转代码。
uni.navigateTo({
//页面路径示例值:/pages/pushinfo/pushinfo
url:'指定页面路径'
})
}
// 监听在线推送消息,若云函数设置了 "force_notification":true,则不会触发此 receive。
if(res.type == 'receive'){
console.log("接收到的消息内容",res.payload);
}
})
0 个回复