Files
medical-mall/uniCloud-alipay/cloudfunctions/testUnipush2/index.js
not-like-juvenile 427010f7db 云服务推送
2026-02-27 16:02:44 +08:00

37 lines
1.3 KiB
JavaScript

// 简单的使用示例
'use strict';
// 可配置的云函数 uni-push 示例:从 event 中读取参数并校验简单 token
const uniPush = uniCloud.getPushManager({ appId: process.env.UNI_PUSH_APPID || "__UNI__9462CA7" });
exports.main = async (event, context) => {
// 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 };
}
};