1import Constants from 'expo-constants'; 2import * as Notifications from 'expo-notifications'; 3 4// In this test app we contact the Expo push service directly. You *never* 5// should do this in a real app. You should always store the push tokens on your 6// own server or use the local notification API if you want to notify this user. 7const PUSH_ENDPOINT = 'https://expo.io/--/api/v2/push/send'; 8 9export default async function registerForPushNotificationsAsync() { 10 // this method assumes the user has already granted permission 11 // to receive remote notifications. 12 13 // Get the token that uniquely identifies this device 14 const { data: token } = await Notifications.getExpoPushTokenAsync(); 15 16 // Log it so we can easily copy it if we need to work with it 17 console.log(`Got this device's push token: ${token}`); 18 19 // POST the token to the Expo push server 20 const response = await fetch(PUSH_ENDPOINT, { 21 method: 'POST', 22 headers: { 23 Accept: 'application/json', 24 'Content-Type': 'application/json', 25 }, 26 body: JSON.stringify([ 27 { 28 to: token, 29 title: 'Welcome to Expo!', 30 body: 'Native Component List is registered for push notifications.', 31 data: { example: 'sample data' }, 32 _category: `${Constants.manifest.id}:welcome`, 33 }, 34 ]), 35 }); 36 37 const result = await response.json(); 38 if (result.errors) { 39 for (const error of result.errors) { 40 console.warn(`API error sending push notification:`, error); 41 } 42 } 43 44 const receipts = result.data; 45 if (receipts) { 46 const receipt = receipts[0]; 47 if (receipt.status === 'error') { 48 if (receipt.details) { 49 console.warn( 50 `Expo push service reported an error sending a notification: ${receipt.details.error}` 51 ); 52 } 53 if (receipt.__debug) { 54 console.warn(receipt.__debug); 55 } 56 } 57 } 58} 59