Note: in the managed workflow, you don't need to pass any additional options to getExpoPushTokenAsync. In the bare workflow, you'll need to pass your experienceId. Make sure you read the documentation for more information.
The following method takes care of all this for you, so feel free to copy/paste it.
registerForPushNotificationsAsync=async()=>{if(Constants.isDevice){const{ status: existingStatus }=awaitNotifications.getPermissionsAsync();let finalStatus = existingStatus;if(existingStatus !=='granted'){const{ status }=awaitNotifications.requestPermissionsAsync();
finalStatus = status;}if(finalStatus !=='granted'){alert('Failed to get push token for push notification!');return;}const token =(awaitNotifications.getExpoPushTokenAsync()).data;console.log(token);this.setState({ expoPushToken: token });}else{alert('Must use physical device for Push Notifications');}if(Platform.OS==='android'){Notifications.setNotificationChannelAsync('default',{
name:'default',
importance:Notifications.AndroidImportance.MAX,
vibrationPattern:[0,250,250,250],
lightColor:'#FF231F7C',});}};
registerForPushNotificationsAsync=async()=>{if(Constants.isDevice){const{ status: existingStatus }=awaitPermissions.getAsync(Permissions.NOTIFICATIONS);let finalStatus = existingStatus;if(existingStatus !=='granted'){const{ status }=awaitPermissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;}if(finalStatus !=='granted'){alert('Failed to get push token for push notification!');return;}const token =awaitNotifications.getExpoPushTokenAsync();console.log(token);this.setState({ expoPushToken: token });}else{alert('Must use physical device for Push Notifications');}if(Platform.OS==='android'){Notifications.createChannelAndroidAsync('default',{
name:'default',
sound:true,
priority:'max',
vibrate:[0,250,250,250],});}};
If you're using the bare workflow, or building a standalone app with expo build:ios or expo build:android, you'll also need to configure the necessary push credentials.
For Android, both managed and bare workflow users need to follow our FCM setup guide, it should only take about 5 minutes.
For iOS, the managed workflow takes care of push notification credentials automatically when you run expo build:ios. In the bare workflow, you'll need to use the expo credentials:manager command to upload your push notification credentials to Expo's servers. Follow these steps:
Make sure your ios.bundleIdentifier key in app.json is set.
Make sure you've created the relevant provisioning profile for your app in the Apple Developer Console
Run expo credentials:manager -p ios in your project directory.
Select Add new Push Notifications Key (or Use existing Push Notifications Key in current project if you already have one)
Note: A paid Apple Developer Account is required to generate credentials.