Device Token Refresh
Firebase periodically rotates FCM registration tokens. When this happens, call this endpoint to swap the old token for the new one without creating a duplicate device record.
FCM Only
This endpoint is only needed for Firebase Cloud Messaging users. OneSignal subscription IDs are stable and do not rotate.
Endpoint
endpoint
http
POST https://api.smartapppush.ai/devices/refresh-tokenAuthentication: api_key in the request body (no Authorization header needed).
Request
request body
json
{
"tenant_id": "your_tenant_id",
"api_key": "your_app_api_key",
"old_token": "expired_fcm_registration_token",
"new_token": "fresh_fcm_registration_token",
"app_user_id": "user_123",
"platform": "android"
}Response
200 OK
json
{
"success": true
}Field Reference
| Field | Required | Description |
|---|---|---|
| tenant_id | Yes | Your account ID |
| api_key | Yes | Your app's API key |
| old_token | Yes | The previous FCM registration token being replaced |
| new_token | Yes | The new FCM registration token from onNewToken() |
| app_user_id | No | Your internal user ID (used for conflict resolution if the new token already exists) |
| platform | No | One of: android, ios, web |
Error Responses
| Status | Meaning |
|---|---|
| 400 | Missing required fields (old_token, new_token) |
| 400 | old_token and new_token must be different |
| 401 | Invalid tenant_id or api_key |
| 404 | Device not found for the given old token |
| 409 | New token is already registered to a different user |
When to Call This
- Firebase triggers
onNewToken()(Android) ormessaging:didReceiveRegistrationToken:(iOS) - Your app retrieves the previous token from local storage and sends both the old and new tokens to this endpoint
- SmartAppPush updates the device record in-place — no duplicate device, no missed pushes
Example Implementation
kotlin
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(newToken: String) {
val oldToken = getStoredToken() // from SharedPreferences
if (oldToken != null && oldToken != newToken) {
val json = JSONObject().apply {
put("tenant_id", "your_tenant_id")
put("api_key", "your_api_key")
put("old_token", oldToken)
put("new_token", newToken)
put("platform", "android")
}
// POST to /devices/refresh-token
postToSmartAppPush(
"https://api.smartapppush.ai/devices/refresh-token",
json
)
}
saveToken(newToken) // update local storage
}
}App Reinstalled?
If the old token is not available (e.g., after a fresh install), skip this endpoint and send a regular event via POST /events instead — it will create the device automatically.