I am using React native with Expo and i want to schedule a notification. I want it to be sent globally at 8pm to all users for all time zones. Can i achieve this with expo? If so, how i can i achieve this? Should i use a local or a push notification? Can someone please point me in the right direction? Thanks in advance!
Question posted in React native
The official React Native documentation can be found here.
The official React Native documentation can be found here.
2
Answers
Yes, it is possible. I can’t tell from your question if you’ve already implemented push notifications for your app and what provider you used. I’ll assume you haven’t at all, so start there.
OneSignal, Firebase, Expo-notifications etc. There are many providers that allow scheduling of push notifications.
OneSignal-Expo documentation:
https://documentation.onesignal.com/docs/react-native-expo-sdk-setup
Expo-server-sdk example:
https://github.com/expo/expo-server-sdk-node#usage
You have two approaches to achieve this:
Local notifications
You can very easily schedule recurring local notification. This means that all users who gives you permission to send them notifications will receive notification at the time you select. Here are some code snippets that should help you:
After you receive permissions you need, you can schedule recuring notifications very easily:
And that’s it! Now your users will receive notification every day.
As you probably see from this example, local notifications have quite a few shortcommings, the biggest is that you need to know which notification you want to send at build time of your app and that you can’t really manipulate with them well.
Use this approach if you want for example to remind users to open your app once a day and do something (good for education apps). For more complicated use cases, you need to use server notifications, which are a lot of harder to implement.
Server notifications
Server notifications are not set in the app, instead, you configure your app as ‘receiver’ of notifications you send from your server. To do this, you need to configure some third party services.
On Android, you won’t avoid using Firebase, as this is the only way for Android devices to receive notifications from your server.
As this requires a lot of code, I will only provide you with directions in this answer. I will stick with Expo Push Notifications as you already use Expo and it’s free (but note that there are other services you can use)
Configure app to receive notifications. See https://docs.expo.dev/versions/latest/sdk/notifications/, specifically check
API/Push Notifications
and how to useNotifications.setNotificationHandler
anduseEffect
method that they use to configure listeners in their App.tsx fileRegister your app on Firebase. Follow this guide to acquire
./google-services.json
and configure yourapp.json
https://docs.expo.dev/push-notifications/using-fcm/Now devices are configured to receive notifications. Last thing you need to do is actually to send it.
And that’s it! Quite a lot of steps for server notifications, but they are more powerful than local as you have complete control over them.