I have been implementing push notifications for my React Native mobile app. But I am having issues now the app is in our staging environment.
When the user signs in, their deviceToken is sent to our backend server, where we can store this data, to use for push notifications later. However, on Android, it is not working as expected. When the user opens the app, there is no prompt asking for permission to send notifications, and as a result, the token is not returned.
My code is something like this:
App.tsx
...
useEffect(() => {
if (Platform.OS !== "web") {
registerForPushNotificationsAsync().then((token) => {
if (token) {
setPushToken(token)
}
})
}
}
...
registerForPushNotificationsAsync()
const registerForPushNotificationsAsync = async () => {
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
})
}
const { status: existingStatus } = await Notifications.getPermissionsAsync()
let finalStatus = existingStatus
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync()
finalStatus = status
}
if (finalStatus !== 'granted') {
return
}
const token = (await Notifications.getDevicePushTokenAsync()).data
return token
}
I don't ever get the prompt to receive notifications locally (via Expo Go) or when I build the APK with our staging credentials.
I can get it to work if I enable notifications in the app's info settings.