eletrotupi / tcc/ api/src/lib/sendPushNotification.ts4e5ac8210eca36b8c2a4073c8eaac1d4998d8039
756 B | 30 lines Raw
1
const EXPO_PUSH_URL = "https://exp.host/--/api/v2/push/send";
2

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

10
export async function sendPushNotification({
11
  token,
12
  title,
13
  body,
14
  data,
15
}: PushPayload): Promise<void> {
16
  console.log(`[push] => token ${token.slice(0, 24)}... title="${title}"`);
17

18
  const response = await fetch(EXPO_PUSH_URL, {
19
    method: "POST",
20
    headers: { "Content-Type": "application/json" },
21
    body: JSON.stringify({ to: token, title, body, data }),
22
  });
23

24
  if (!response.ok) {
25
    const errBody = await response.text();
26

27
    console.error(`[push] Expo API ${response.status}: ${errBody}`);
28
    throw new Error(`Expo push API returned ${response.status}`);
29
  }
30
}