云服务推送
This commit is contained in:
@@ -1,5 +1,37 @@
|
||||
// 简单的使用示例
|
||||
'use strict';
|
||||
// 可配置的云函数 uni-push 示例:从 event 中读取参数并校验简单 token
|
||||
const uniPush = uniCloud.getPushManager({ appId: process.env.UNI_PUSH_APPID || "__UNI__9462CA7" });
|
||||
|
||||
exports.main = async (event, context) => {
|
||||
console.log('test run event:', event);
|
||||
return { ok:true, now: Date.now(), event };
|
||||
// event: { token, push_clientid, title, content, payload }
|
||||
const { token, push_clientid, title = '', content = '', payload = {} } = event || {};
|
||||
|
||||
// 简单鉴权,建议把真实 secret 存到云函数环境变量 PUSH_TOKEN
|
||||
const SECRET = process.env.PUSH_TOKEN;
|
||||
if (SECRET && token !== SECRET) {
|
||||
console.warn('unauthorized token', { hasToken: !!token });
|
||||
return { errCode: 401, errMsg: 'unauthorized' };
|
||||
}
|
||||
|
||||
if (!push_clientid) {
|
||||
return { errCode: 400, errMsg: 'push_clientid required' };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('sending uni-push', { push_clientid, title, content, payload });
|
||||
const res = await uniPush.sendMessage({
|
||||
push_clientid,
|
||||
force_notification: true,
|
||||
title,
|
||||
content,
|
||||
settings: { ttl: 86400000 },
|
||||
payload
|
||||
});
|
||||
console.log('uni-push response', res);
|
||||
return { errCode: 0, errMsg: 'success', data: res };
|
||||
} catch (e) {
|
||||
console.error('uni-push send error', e && (e.stack || e.message || e));
|
||||
return { errCode: 500, errMsg: e && e.message ? e.message : 'push error', detail: e };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
// 本文件中的json内容将在云函数【运行】时作为参数传给云函数。
|
||||
// 配置教程参考:https://uniapp.dcloud.net.cn/uniCloud/rundebug.html#runparam
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user