App Push Notifications in app
Step 1
The app integration will vary between projects and platforms. Please follow guides at 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, and entry points for opening the app from a notification and handling incoming notifications while the app is running.
Step 2
When the app user enables notifications, you will need the device FCM token to subscribe or unsubscribe.
In React Native, after having requested permissions from the user, you will get the token by importing the library:
import messaging from '@react-native-firebase/messaging';
and call
const token = await messaging().getToken();
For other platforms, like iOS, Android or Flutter, there will be similar methods available.
Step 3
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.
- Method:
POST
- Route:
{baseUrl}/api/v1/app-push/subscribe
- Authorization: Bearer token (using the M2M generated in Basic setup, step 4)
- Body: See example below
Example of body :
{
"provider": "fcm",
"token": "INSERT FCM TOKEN HERE",
"topics": [
"politics"
]
}
You can use the same call to update the subscription with more or less topics.
Step 4
To unsubscribe from notifications, when the user toggles them off, send the same token to the Subrite API, this time as a DELETE
-request.
- Method:
DELETE
- Route:
{baseUrl}/api/v1/app-push/subscribe
- Authorization: Bearer token (using the M2M generated in Basic setup, step 4)
- Body: See example below
Example of body :
{
"provider": "fcm",
"token": "INSERT TOKEN HERE"
}
Step 5 (Optional)
If your app supports login with the Subrite platform, you can link a subscriber with a member. You will not use the M2M-token for this, you will use the JWT-token provided by Subrite when the user logs in.
- Method:
PUT
- Route:
{baseUrl}/api/v1/app-push/linksubscriber
- Authorization: Bearer token (using the user's JWT token)
- Body: See example below
Example of body :
{
"provider": "fcm",
"token": "INSERT TOKEN HERE"
}
Step 6 (Optional)
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 how to get 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
});
- Method:
PUT
- Route:
{baseUrl}/api/v1/app-push/open
- Authorization: Bearer token (using the M2M generated in Basic setup, step 4)
- Body: See example below
Example of body :
{
"itemKey": "a8786532953ba69c7793a5d85c06c84eea1f25eb"
}
Now the app integration should be ready, and you can continue with integrating the server.