mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 01:02:55 +00:00
refactor: dedupe APNs push send flow and add wake default test
This commit is contained in:
@@ -425,6 +425,46 @@ function toApnsPushResult(params: {
|
||||
};
|
||||
}
|
||||
|
||||
function createOpenClawPushMetadata(params: {
|
||||
kind: "push.test" | "node.wake";
|
||||
nodeId: string;
|
||||
reason?: string;
|
||||
}): { kind: "push.test" | "node.wake"; nodeId: string; ts: number; reason?: string } {
|
||||
return {
|
||||
kind: params.kind,
|
||||
nodeId: params.nodeId,
|
||||
ts: Date.now(),
|
||||
...(params.reason ? { reason: params.reason } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function sendApnsPush(params: {
|
||||
auth: ApnsAuthConfig;
|
||||
registration: ApnsRegistration;
|
||||
payload: object;
|
||||
timeoutMs?: number;
|
||||
requestSender?: ApnsRequestSender;
|
||||
pushType: ApnsPushType;
|
||||
priority: "10" | "5";
|
||||
}): Promise<ApnsPushWakeResult> {
|
||||
const { token, topic, environment, bearerToken } = resolveApnsSendContext({
|
||||
auth: params.auth,
|
||||
registration: params.registration,
|
||||
});
|
||||
const sender = params.requestSender ?? sendApnsRequest;
|
||||
const response = await sender({
|
||||
token,
|
||||
topic,
|
||||
environment,
|
||||
bearerToken,
|
||||
payload: params.payload,
|
||||
timeoutMs: resolveApnsTimeoutMs(params.timeoutMs),
|
||||
pushType: params.pushType,
|
||||
priority: params.priority,
|
||||
});
|
||||
return toApnsPushResult({ response, token, topic, environment });
|
||||
}
|
||||
|
||||
export async function sendApnsAlert(params: {
|
||||
auth: ApnsAuthConfig;
|
||||
registration: ApnsRegistration;
|
||||
@@ -434,11 +474,6 @@ export async function sendApnsAlert(params: {
|
||||
timeoutMs?: number;
|
||||
requestSender?: ApnsRequestSender;
|
||||
}): Promise<ApnsPushAlertResult> {
|
||||
const { token, topic, environment, bearerToken } = resolveApnsSendContext({
|
||||
auth: params.auth,
|
||||
registration: params.registration,
|
||||
});
|
||||
|
||||
const payload = {
|
||||
aps: {
|
||||
alert: {
|
||||
@@ -447,31 +482,21 @@ export async function sendApnsAlert(params: {
|
||||
},
|
||||
sound: "default",
|
||||
},
|
||||
openclaw: {
|
||||
openclaw: createOpenClawPushMetadata({
|
||||
kind: "push.test",
|
||||
nodeId: params.nodeId,
|
||||
ts: Date.now(),
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
const sender = params.requestSender ?? sendApnsRequest;
|
||||
const response = await sender({
|
||||
token,
|
||||
topic,
|
||||
environment,
|
||||
bearerToken,
|
||||
return await sendApnsPush({
|
||||
auth: params.auth,
|
||||
registration: params.registration,
|
||||
payload,
|
||||
timeoutMs: resolveApnsTimeoutMs(params.timeoutMs),
|
||||
timeoutMs: params.timeoutMs,
|
||||
requestSender: params.requestSender,
|
||||
pushType: "alert",
|
||||
priority: "10",
|
||||
});
|
||||
|
||||
return toApnsPushResult({
|
||||
response,
|
||||
token,
|
||||
topic,
|
||||
environment,
|
||||
});
|
||||
}
|
||||
|
||||
export async function sendApnsBackgroundWake(params: {
|
||||
@@ -482,39 +507,23 @@ export async function sendApnsBackgroundWake(params: {
|
||||
timeoutMs?: number;
|
||||
requestSender?: ApnsRequestSender;
|
||||
}): Promise<ApnsPushWakeResult> {
|
||||
const { token, topic, environment, bearerToken } = resolveApnsSendContext({
|
||||
auth: params.auth,
|
||||
registration: params.registration,
|
||||
});
|
||||
|
||||
const payload = {
|
||||
aps: {
|
||||
"content-available": 1,
|
||||
},
|
||||
openclaw: {
|
||||
openclaw: createOpenClawPushMetadata({
|
||||
kind: "node.wake",
|
||||
reason: params.wakeReason ?? "node.invoke",
|
||||
nodeId: params.nodeId,
|
||||
ts: Date.now(),
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
const sender = params.requestSender ?? sendApnsRequest;
|
||||
const response = await sender({
|
||||
token,
|
||||
topic,
|
||||
environment,
|
||||
bearerToken,
|
||||
return await sendApnsPush({
|
||||
auth: params.auth,
|
||||
registration: params.registration,
|
||||
payload,
|
||||
timeoutMs: resolveApnsTimeoutMs(params.timeoutMs),
|
||||
timeoutMs: params.timeoutMs,
|
||||
requestSender: params.requestSender,
|
||||
pushType: "background",
|
||||
priority: "5",
|
||||
});
|
||||
|
||||
return toApnsPushResult({
|
||||
response,
|
||||
token,
|
||||
topic,
|
||||
environment,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user