Integrate push notifications in your app
Get the device FCM token, subscribe and unsubscribe devices with optional topics, link a subscriber to a logged-in member, and report when a user opens the app from a notification.
Get the device FCM token, subscribe and unsubscribe devices with optional topics, link a subscriber to a logged-in member, and report when a user opens the app from a notification.
Stay in the loop 🚀
The app integration varies between projects and platforms. Follow the guides in the Firebase docs to set up Firebase for your platform of choice.
There should be functionality in place in the app to toggle notifications on and off, entry points for opening the app from a notification, and handling of incoming notifications while the app is running.
When the app user enables notifications, you need the device FCM token to subscribe or unsubscribe.
In React Native, after requesting permissions from the user, you get the token by importing the library and calling getToken():
import messaging from '@react-native-firebase/messaging';
const token = await messaging().getToken();Other platforms, like iOS, Android or Flutter, have similar methods available.
To subscribe to notifications when the user toggles them on, send the token to the Subrite API. There is an optional array of strings, topics, if you want your users to be able to subscribe to specific topics of their interest.
POST{baseUrl}/api/v1/app-push/subscribe{
"provider": "fcm",
"token": "INSERT FCM TOKEN HERE",
"topics": [
"politics"
]
}You can use the same call to update the subscription with more or fewer topics.
While topic filtering is on (the default), topics only work if both sides agree:
topics array, or with topics that never match, only receives notifications sent without topics.To unsubscribe from notifications when the user toggles them off, send the same token to the Subrite API, this time as a DELETE request.
DELETE{baseUrl}/api/v1/app-push/subscribe{
"provider": "fcm",
"token": "INSERT TOKEN HERE"
}If your app supports login with the Subrite platform, you can link a subscriber with a member. You do not use the M2M token for this. Use the JWT that Subrite provides when the user logs in.
PUT{baseUrl}/api/v1/app-push/linksubscriber{
"provider": "fcm",
"token": "INSERT TOKEN HERE"
}To track a user engaging with the notification, you can notify the Subrite API whenever a user opens the app from a notification. In the payload of the notification, you will find an itemKey.
Example React Native code for getting the itemKey:
// from background state
messaging().onNotificationOpenedApp(
message => {
const itemKey = message?.data?.itemKey;
// send to Subrite API here
}
)
// from quit state
messaging()
.getInitialNotification()
.then(message => {
const itemKey = message?.data?.itemKey;
// send to Subrite API here
});PUT{baseUrl}/api/v1/app-push/open{
"itemKey": "a8786532953ba69c7793a5d85c06c84eea1f25eb"
}The app integration is now ready, and you can continue with .