1--- 2title: Push Notifications Setup 3sidebar_title: Setup 4--- 5 6import { Tab, Tabs } from '~/ui/components/Tabs'; 7 8To get the client-side ready for push notifications, the 2 main things we need are: 9 10- The user's permission to send them push notifications 11- The user's ExpoPushToken- if push notifications are mail, then the ExpoPushToken is the user's address. 12 13We can easily grab both of these using the `expo-notifications` library. For permissions, use [`requestPermissionsAsync`](../versions/latest/sdk/notifications.mdx#requestpermissionsasyncrequest-notificationpermissionsrequest-promisenotificationpermissionsstatus), and for the ExpoPushToken, use [`getExpoPushTokenAsync`](../versions/latest/sdk/notifications.mdx#getexpopushtokenasyncoptions-expotokenoptions-expopushtoken). 14 15> 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. 16 17The following method takes care of all this for you, so feel free to copy/paste it. 18 19<Tabs> 20<Tab label="New notifications"> 21 22{/* prettier-ignore */} 23```javascript 24registerForPushNotificationsAsync = async () => { 25 /* @info We should also make sure the app is running on a physical device, since push notifications won't work on a simulator. */ 26 if (Device.isDevice) { 27 /* @end */ 28 const { status: existingStatus } = await Notifications.getPermissionsAsync(); 29 let finalStatus = existingStatus; 30 if (existingStatus !== 'granted') { 31 const { status } = await Notifications.requestPermissionsAsync(); 32 finalStatus = status; 33 } 34 if (finalStatus !== 'granted') { 35 alert('Failed to get push token for push notification!'); 36 return; 37 } 38 /* @info Alright, we got our token! */ 39 const token = (await Notifications.getExpoPushTokenAsync()).data; 40 /* @end */ 41 console.log(token); 42 this.setState({ expoPushToken: token }); 43 } else { 44 alert('Must use physical device for Push Notifications'); 45 } 46 47 /* @info On Android, we need to specify a channel. Find out more specifics in the expo-notifications documentation. */ 48 if (Platform.OS === 'android') { 49 Notifications.setNotificationChannelAsync('default', { 50 name: 'default', 51 importance: Notifications.AndroidImportance.MAX, 52 vibrationPattern: [0, 250, 250, 250], 53 lightColor: '#FF231F7C', 54 }); 55 } 56 /* @end */ 57}; 58``` 59 60</Tab> 61<Tab label="Legacy notifications"> 62 63{/* prettier-ignore */} 64```javascript 65registerForPushNotificationsAsync = async () => { 66 /* @info We should also make sure the app is running on a physical device, since push notifications won't work on a simulator. */ 67 if (Device.isDevice) { 68 /* @end */ 69 const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS); 70 let finalStatus = existingStatus; 71 if (existingStatus !== 'granted') { 72 const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); 73 finalStatus = status; 74 } 75 if (finalStatus !== 'granted') { 76 alert('Failed to get push token for push notification!'); 77 return; 78 } 79 /* @info Alright, we got our token! */ 80 const token = await Notifications.getExpoPushTokenAsync(); 81 /* @end */ 82 console.log(token); 83 this.setState({ expoPushToken: token }); 84 } else { 85 alert('Must use physical device for Push Notifications'); 86 } 87 88 /* @info On Android, we need to specify a channel. Find out more specifics in the expo-notifications documentation. */ 89 if (Platform.OS === 'android') { 90 Notifications.createChannelAndroidAsync('default', { 91 name: 'default', 92 sound: true, 93 priority: 'max', 94 vibrate: [0, 250, 250, 250], 95 }); 96 } 97 /* @end */ 98}; 99``` 100 101</Tab> 102</Tabs> 103 104## Credentials 105 106For Android, both managed and bare workflow users need to follow our [FCM setup guide](using-fcm.mdx), it should only take about 5 minutes. 107 108For iOS, EAS Build will take care of push notification credentials automatically when you run a build. If are you using bare workflow and not building with EAS Build, you will need to run `eas credentials` manually. 109 110> A paid Apple Developer Account is **required** to generate credentials. 111 112## Next steps 113 114Try out [sending a notification with Expo](./sending-notifications.mdx)! 115 116## See also 117 118- Having trouble? Visit [Expo's notification FAQ page](./faq.mdx) 119