Create sendPushNotification method
This commit is contained in:
parent
5fb2293617
commit
af8da9a273
@ -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());
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user