eletrotupi / tcc/ api/src/lib/sendPushNotification.tsc901c35af9c84b6a29e5185545ced1129f33b8f4
863 B | 38 lines Raw
1
import { logger } from "@app/lib/logger";
2

3
const EXPO_PUSH_URL = "https://exp.host/--/api/v2/push/send";
4

5
type PushPayload = {
6
  token: string;
7
  title: string;
8
  body: string;
9
  data?: Record<string, unknown>;
10
};
11

12
export async function sendPushNotification({
13
  token,
14
  title,
15
  body,
16
  data,
17
}: PushPayload): Promise<void> {
18
  logger.info(
19
    { tokenPrefix: token.slice(0, 24), title },
20
    "[push] sending notification",
21
  );
22

23
  const response = await fetch(EXPO_PUSH_URL, {
24
    method: "POST",
25
    headers: { "Content-Type": "application/json" },
26
    body: JSON.stringify({ to: token, title, body, data }),
27
  });
28

29
  if (!response.ok) {
30
    const errBody = await response.text();
31

32
    logger.error(
33
      { status: response.status, body: errBody },
34
      "[push] Expo API error",
35
    );
36
    throw new Error(`Expo push API returned ${response.status}`);
37
  }
38
}