19155fd2fSŁukasz Kosmatyimport * as Notifications from 'expo-notifications';
269185572SBartłomiej Bukowski
369185572SBartłomiej Bukowski// In this test app we contact the Expo push service directly. You *never*
469185572SBartłomiej Bukowski// should do this in a real app. You should always store the push tokens on your
569185572SBartłomiej Bukowski// own server or use the local notification API if you want to notify this user.
6*6a43abdcSBrent Vatneconst PUSH_ENDPOINT = 'https://exp.host/--/api/v2/push/send';
769185572SBartłomiej Bukowski
88c3c9794SEric Samelsonexport default async function registerForPushNotificationsAsync() {
969185572SBartłomiej Bukowski  // this method assumes the user has already granted permission
109155fd2fSŁukasz Kosmaty  // to receive remote notifications.
1169185572SBartłomiej Bukowski
1269185572SBartłomiej Bukowski  // Get the token that uniquely identifies this device
139155fd2fSŁukasz Kosmaty  const { data: token } = await Notifications.getExpoPushTokenAsync();
1469185572SBartłomiej Bukowski
1569185572SBartłomiej Bukowski  // Log it so we can easily copy it if we need to work with it
1669185572SBartłomiej Bukowski  console.log(`Got this device's push token: ${token}`);
1769185572SBartłomiej Bukowski
1869185572SBartłomiej Bukowski  // POST the token to the Expo push server
1969185572SBartłomiej Bukowski  const response = await fetch(PUSH_ENDPOINT, {
2069185572SBartłomiej Bukowski    method: 'POST',
2169185572SBartłomiej Bukowski    headers: {
229155fd2fSŁukasz Kosmaty      Accept: 'application/json',
2369185572SBartłomiej Bukowski      'Content-Type': 'application/json',
2469185572SBartłomiej Bukowski    },
2569185572SBartłomiej Bukowski    body: JSON.stringify([
2669185572SBartłomiej Bukowski      {
2769185572SBartłomiej Bukowski        to: token,
288c3c9794SEric Samelson        title: 'Welcome to Expo!',
298c3c9794SEric Samelson        body: 'Native Component List is registered for push notifications.',
308c3c9794SEric Samelson        data: { example: 'sample data' },
31eb3d4616Scruzach        categoryId: 'welcome',
3269185572SBartłomiej Bukowski      },
3369185572SBartłomiej Bukowski    ]),
3469185572SBartłomiej Bukowski  });
3569185572SBartłomiej Bukowski
3669185572SBartłomiej Bukowski  const result = await response.json();
3769185572SBartłomiej Bukowski  if (result.errors) {
3869185572SBartłomiej Bukowski    for (const error of result.errors) {
3969185572SBartłomiej Bukowski      console.warn(`API error sending push notification:`, error);
4069185572SBartłomiej Bukowski    }
4169185572SBartłomiej Bukowski  }
4269185572SBartłomiej Bukowski
4369185572SBartłomiej Bukowski  const receipts = result.data;
4469185572SBartłomiej Bukowski  if (receipts) {
4569185572SBartłomiej Bukowski    const receipt = receipts[0];
4669185572SBartłomiej Bukowski    if (receipt.status === 'error') {
4769185572SBartłomiej Bukowski      if (receipt.details) {
4869185572SBartłomiej Bukowski        console.warn(
499155fd2fSŁukasz Kosmaty          `Expo push service reported an error sending a notification: ${receipt.details.error}`
5069185572SBartłomiej Bukowski        );
5169185572SBartłomiej Bukowski      }
5269185572SBartłomiej Bukowski      if (receipt.__debug) {
5369185572SBartłomiej Bukowski        console.warn(receipt.__debug);
5469185572SBartłomiej Bukowski      }
5569185572SBartłomiej Bukowski    }
5669185572SBartłomiej Bukowski  }
5769185572SBartłomiej Bukowski}
58