push-notifications
#
Installation#
Firebase Cloud Messaging (FCM)FCM is required for sending push notifications to Android devices. See Expo docs.
- Add Project in Firebase Console
- Add Firebase to your Android app and follow the setup steps. Make sure that the Android package name you enter is the same as the value of android.package in your app.config.*.ts
- Download the google-services.json file and place it in
src/constants/google-services/google-services-*.json
- In your app.config.*.ts, add an android.googleServicesFile field with the relative path to the
google-services.json
file you just downloaded.
#
APIThis module can used either with context or hooks.
Note: When reading the docs below, pay attention to the difference between these:
registerForPushNotifications
-> this is a function you can pass in to customize getting the tokenregisterPushNotifications
-> this is a function you get back to trigger getting the tokenonRegisterPushNotifications
-> this is a callback whenregisterPushFunction
resolves
#
NotificationProviderTakes in functions to handle:
- notification received
- notification response received
- save token / register device
#
NotificationPromptOn render, prompts the user for permission to send notifications. Uses context from
NotificationProvider
.
#
NotificationAlertPromptIf permission is undetermined, it renders an alert. If the user accepts, then it requests permission to notify. On iOS if the user denies you cannot ask for permission again. You can customize the alert copy, defaults are shown below.
NOTE: On iOS if the user denies the status is always undetermined, you may want to store if the
user denied and hide the component. You can do this using onRegisterPushNotifications
on
NotificationProvider
or onRegisterPushNotifications
on NotificationAlertPrompt
. In the future
we will handle this here. Note that onRegisterPushNotifications
should be a stable function
(should not change on render).
#
NotificationRenderPromptIf permission is undetermined, it renders the children. The children is a function that gets passed
in permissionStatus
and registerPushNotifications
.
NOTE: On iOS if the user denies the status is always undetermined, you may want to store if the
user denied and hide the component. You can check the result of registerPushNotifications
for
this.
#
usePushNotificationsYou can avoid the context entirely and use the hooks instead.