When building mobile applications, notifications are a crucial feature, helping to keep users engaged and updated. If you’re using Expo and a NestJS backend to handle push notifications, you might have noticed a strange quirk: notifications work perfectly fine in development but mysteriously fail in preview builds.
That was exactly the issue I faced while working on my app. Everything was going smoothly during development, but once I moved to preview builds, the push notifications simply wouldn’t come through. After spending hours troubleshooting, I finally figured out the problem — the Project ID.
This article will walk you through the issue and provide the solution I found, saving you hours of head-scratching.
The Problem: Notifications Work in Development, But Not in Preview
While setting up push notifications using Expo, I used the following code to retrieve the Expo push token:
expoPushToken = (await Notifications.getExpoPushTokenAsync()).data;
This code worked perfectly in development. I was getting Expo push tokens and receiving notifications as expected. However, when I created a preview build, notifications stopped working altogether.
Why It Worked in Development but Not in Preview
When you run an Expo app in development, the Project ID is automatically set, allowing everything to function smoothly, including retrieving push tokens.
But here’s the catch: in preview builds (or non-development environments), the Project ID isn’t set automatically. When it’s missing, the app doesn’t know which Expo project it belongs to, and as a result, the Expo push token can’t be retrieved properly.
This was the root cause of my issue. The Project ID was missing when I ran the app in preview mode.
The Solution: Manually Set the Project ID
To fix this issue, you need to explicitly provide the Project ID when retrieving the Expo push token. Here’s the code that worked for me:
token = await Notifications.getExpoPushTokenAsync({
projectId: Constants.expoConfig.extra.eas.projectId,
});
This ensures that even in a preview build, the app knows which project it belongs to by using the projectId stored in your Expo configuration.
The Final Code: Error Handling and Push Token Retrieval
Incorporating error handling and fallback options, here’s the final code I used to ensure that notifications work both in development and in preview builds:
try {
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.easConfig?.projectId;
if (!projectId) {
throw new Error('Project ID not found');
}
expoPushToken = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
} catch (error) {
console.error('Error retrieving Expo push token:', error);
}
How It Works:
- Project ID Retrieval: We first try to retrieve the Project ID from
Constants.expoConfig.extra.eas.projectId
. If it’s not found, we checkConstants.easConfig.projectId
. - Error Handling: If no Project ID is found, an error is thrown to ensure we catch it early.
- Get Expo Push Token: Finally, we pass the Project ID into
Notifications.getExpoPushTokenAsync
to retrieve the token.
This solution ensures that the Project ID is always provided, regardless of whether you’re in development or preview mode.
Why This Works
- Development Build: The Project ID is automatically set when using development builds, so you don’t need to worry about this in local development.
- Preview/Production Builds: In preview and production builds, the Project ID isn’t automatically included, which is why you need to manually fetch it from your Expo configuration.
By manually specifying the Project ID, you’re telling the Expo Notifications service which project the push token belongs to, ensuring notifications work in all environments.
Conclusion
If you’re working with Expo notifications and find that everything works perfectly in development but fails in preview builds, the missing Project ID could be the culprit. By explicitly passing the Project ID when retrieving the push token, you can ensure that push notifications work across all environments.
Notifications are an essential part of many mobile apps, and ensuring they work in both development and preview builds is critical. Hopefully, this solution saves you the time and frustration I experienced!
This should help you set up Expo notifications properly for both development and production environments. Feel free to share this article with fellow developers who might be facing the same issue!