If you use FCM APIs to build send requests programmatically, you may find that, over time, you are wasting resources by sending messages to inactive devices with stale registration tokens. This situation can affect the message delivery data reported in the Firebase console or data exported to BigQuery, showing up as a dramatic (but not actually valid) drop in delivery rates. This guide discusses some measures you can take to help ensure efficient message targeting and valid delivery reporting.
Basic best practices
There are some fundamental practices you should follow in any app that uses FCM APIs to build send requests programmatically. The main best practices are:
- Store registration tokens on your server. An important role for the server is to keep track of each client's token and keep an updated list of active tokens. We strongly recommend implementing a token timestamp in your code and your servers, and updating this timestamp at regular intervals.
- Remove stored tokens that become stale. In addition to removing tokens in obvious cases of invalid token responses, you'll probably need to monitor other signs that the token is stale. This guide discusses some of your options for achieving this.
Retrieve and store registration tokens
On initial startup of your app, the FCM SDK generates a registration token for the client app instance. This is the token that you must include in targeted send requests from the API, or add to topic subscriptions for targeting topics.
As noted in our client setup guides, your app should retrieve this token at initial startup and save it to your app server alongside a timestamp. This timestamp must be implemented by your code and your servers, as it is not provided for you by FCM SDKs.
Also, it's important to save the token to the server and update the timestamp whenever it changes, such as when:
- The app is restored on a new device
- The user uninstalls/reinstall the app
- The user clears app data.
Detect invalid token responses from the FCM backend
Make sure to detect invalid token responses from FCM and respond by deleting from your system any registration tokens that are known to be invalid. With the HTTP v1 API, these error messages may indicate that your send request targeted stale or invalid tokens:
See ErrorCodes for more information.
If you receive either of these responses for a targeted token, it is safe to delete your record of this token, since it will never again be valid. However, keep in mind that there will still be cases where a token is in fact invalid, but there is no indication of it. For example, sometimes the FCM backend cannot verify whether or not a device has gone offline permanently.
Ensuring registration token freshness
Determining whether a token is fresh or stale is not always straightforward. To cover all cases, you should adopt a threshold for when you consider tokens stale; our recommendation is two months. Any token older than two months is likely to be an inactive device; an active device would have otherwise refreshed its token.
Update tokens on a regular basis
We recommend that you periodically retrieve and update all registration tokens on your server. This requires you to:
- Add app logic in your client app to retrieve the current token using the appropriate API call (such as
token(completion):for iOS or
getToken()for Android) and then send the current token to your app server for storage (with timestamp). This could be a monthly job configured to cover all clients/tokens.
- Add server logic to update the token’s timestamp at regular intervals, regardless of whether or not the token has changed.
Whatever timing pattern you follow, make sure to update tokens periodically. An update frequency of once per month likely strikes a good balance between battery impact vs. detecting inactive registration tokens. By doing this refresh, you also ensure that any device which goes inactive will refresh its registration when it becomes active again. There is no benefit to doing the refresh more frequently than weekly.
Unsubscribe stale tokens from topics
Managing topics subscriptions to remove stale registration tokens is another consideration. It involves two steps:
- Your app should resubscribe to topics once per month and/or whenever the registration token changes. This forms a self-healing solution, where the subscriptions reappear automatically when an app becomes active again.
- If an app instance is idle for 2 months (or your own staleness window) you should unsubscribe it from topics using the Firebase Admin SDK to delete the token/topic mapping from the FCM backend.
The benefit of these two steps is that your fanouts will occur faster since there are fewer stale tokens to fan out to, and your stale app instances will automatically resubscribe once they are active again.
Measuring delivery success
Generally, we advise targeting messages based on actions observed or captured from actively used app instances. This is especially important if you regularly send messages to topics with large numbers of subscribers; if a portion of those subscribers are actually inactive, the impact on your delivery statistics can be significant over time.
Before targeting messages to a token, consider:
- Do Google Analytics, data captured in BigQuery, or other tracking signals indicate the token is active?
- Have previous delivery attempts failed consistently over a period of time?
- Has the registration token been updated on your servers in the past two months?
- For Android devices, does the FCM Data API report a high percentage of message delivery failures due to
For more information about delivery, see Understanding message delivery.