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-token

Authentication: 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

FieldRequiredDescription
tenant_idYesYour account ID
api_keyYesYour app's API key
old_tokenYesThe previous FCM registration token being replaced
new_tokenYesThe new FCM registration token from onNewToken()
app_user_idNoYour internal user ID (used for conflict resolution if the new token already exists)
platformNoOne of: android, ios, web

Error Responses

StatusMeaning
400Missing required fields (old_token, new_token)
400old_token and new_token must be different
401Invalid tenant_id or api_key
404Device not found for the given old token
409New token is already registered to a different user

When to Call This

  1. Firebase triggers onNewToken() (Android) or messaging:didReceiveRegistrationToken: (iOS)
  2. Your app retrieves the previous token from local storage and sends both the old and new tokens to this endpoint
  3. 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.

We use cookies to understand how you use our site and improve your experience. No personal data is sold or shared.