From af8da9a273bec2ba3ede747f1efe9a9b4b1d63c0 Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:51:11 -0400 Subject: [PATCH] Create sendPushNotification method --- functions/gcloud.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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()); +}