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