diff --git a/functions/gcloud.ts b/functions/gcloud.ts index 676594b..5de325b 100644 --- a/functions/gcloud.ts +++ b/functions/gcloud.ts @@ -269,3 +269,32 @@ export async function queryLogs(user: number, context: RequestContext) { ).batch?.entityResults ?? [] ); } + +export async function sendPushNotification( + env: Env, + title: string, + body: string, + token?: string, +) { + const message = JSON.stringify({ + notification: { + body, + title, + }, + token, + }); + + const notifResp = await fetch( + "https://fcm.googleapis.com/v1/projects/car-crushers-mobile/messages:send", + { + body: JSON.stringify({ message }), + headers: { + authorization: `Bearer ${await GetAccessToken(env)}`, + "content-type": "application/json", + }, + method: "POST", + }, + ); + + if (!notifResp.ok) throw new Error(await notifResp.json()); +}