Create sendPushNotification method

This commit is contained in:
regalijan 2023-10-19 16:51:11 -04:00
parent 5fb2293617
commit af8da9a273
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -269,3 +269,32 @@ export async function queryLogs(user: number, context: RequestContext) {
).batch?.entityResults ?? [] ).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());
}