Push Token
The SDK does not include Firebase. Your app owns Firebase Cloud Messaging and passes the FCM token to the SDK, which registers it on the current contact. How push delivery works in general: Push Notifications.
Requirements
- SDK 2.0.0 or later. If you are upgrading from 1.x, see the Migration Guide.
- Firebase Cloud Messaging added to your app, with
google-services.json, see the Firebase guide. - The same Firebase project's service account key uploaded to the Positive User panel.
Setup
-
Add Firebase Cloud Messaging and a
FirebaseMessagingServiceas described in the Firebase guide. -
Pass the token at launch and every new token Firebase reports:
// Application.onCreate, after UserCom.initializeFirebaseMessaging.getInstance().token.addOnSuccessListener { token ->UserCom.default?.setFcmToken(token)}// FirebaseMessagingServiceoverride fun onNewToken(token: String) {UserCom.default?.setFcmToken(token)} -
When a contact logs in, pass the token together with the identity. See Login and logout.
The SDK stores the token on the device and sends it with every later ping. Passing it at launch makes sure the SDK always has the current token, also after an update from an SDK version that fetched the token itself.
After the first launch with a token, a push notification sent to this contact arrives on the device. The panel does not show the push token itself. To check that devices have one, go to Data → Contacts and filter contacts by FCM key with the condition has any value.
Ways to pass the token
All of these store the token and attach it to pings. Pick the one that matches the moment you have the token.
| When | Call | Sends a ping |
|---|---|---|
| Token known at launch | UserCom.initialize(application, apiKey, baseUrl, fcmToken) | No. Call ping() yourself. |
| Token arrives or rotates | setFcmToken(token) | Yes |
| You ping anyway | ping(fcmToken, callback) | Yes, this call is the ping |
| Login or profile update | updateContact(contact, fcmToken, callback) | Yes, contact data and token in one request |
Login and logout
At login, send the contact data and the token in one call:
val contact = Contact(userId = "12345", email = "jane@example.com")
UserCom.default?.updateContact(contact, token)
Two separate calls (updateContact and then setFcmToken) send two pings. One call sends the identity and the token together.
At logout, pass the token you want to remove from the contact who is leaving. Without a token, the SDK skips the removal:
UserCom.default?.logout(token) { error ->
// a new anonymous contact is ready
}
The anonymous contact created by logout gets the same token, so the device keeps receiving push notifications. Turning them off after logout is up to your app. Details: Contacts.
Methods
setFcmToken
Stores the FCM token and sends a ping, which registers the token on the current contact. Call it whenever Firebase reports a new token, and at launch.
Optional unless marked required
- token
- The FCM registration token from Firebase.
Returns nothing. The ping goes through the SDK's request queue. Every call sends a ping, also with an unchanged token. Errors go to UserCom.errorHandler. To read the result, use ping(fcmToken, callback) instead.
- Kotlin
- Java
UserCom.default?.setFcmToken(token)
UserCom sdk = UserCom.getDefault();
if (sdk != null) sdk.setFcmToken(token);
The other methods that accept a token are described with their topic: ping, updateContact, logout.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| The contact cannot be selected in a push campaign | The contact has no device with a push token: no FCM token was passed to the SDK, or the token moved to another contact that later used the same device. | Call setFcmToken at launch and from onNewToken. |
| No token after updating from 1.x | 1.x fetched the token itself. 2.0.0 sends only the token your app passes. | Add the calls from Setup. |
| Delivery fails with a sender ID mismatch | The app uses a different Firebase project than the service account key in the panel. | Upload the service account key of the project in your app's google-services.json. |