firebase / firebase/firebase-android-sdk

FirebaseMessagingService: onNewToken() for non-default FirebaseApp

Open
#5,646 1 comment 0 reactions 0 assignees View on GitHub
api: messaging type: feature request
Dominant language
Java
Stars
2.6k
Forks
710
Avg merge
2d 23h
Merged PRs (30d)
34

Description

## What feature would you like to see?

The [`FirebaseMessagingService.onNewToken(token: String)`](https://github.com/firebase/firebase-android-sdk/blob/4dcae02e0937b0fc347e7aeab971a77fcf3762e8/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java#L153C20-L153C20) method is called when a FCM token is (re)generated, but only for the default FirebaseApp, as defined in [FirebaseMessaging.invokeOnTokenRefresh](https://github.com/firebase/firebase-android-sdk/blob/4dcae02e0937b0fc347e7aeab971a77fcf3762e8/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java#L647).

A new method `onNewToken(token: String, appName: String)` could be added to the FirebaseMessagingService to handle token (re)generation for non-default FirebaseApp instances. This way the new overloaded method gets called on token generation for all FirebaseApp instances, allowing clients like me to handle/save the new token.

The current `onNewToken` could remain exclusive to the default app so as to not introduce breaking changes.

## How would you use it?

I can see the two following use cases:

**1/** Having two or more Firebase projects registered and being able to send notifications and messages from both.

**2/** (My use case) I am working on an easy-to-use custom notification server, with the objective that anyone can host their own instance (e.g. private Nagios instance / home automation). \
The Android app therefore needs to receive Firebase project parameters from the server and initialize a secondary FirebaseApp at runtime. Once initialized, this instance should be able to handle the entire FCM token lifecycle, the regeneration of which is a crutial step.

The current workaround I am using is storing the last known token in DataStore preferences and having a worker check whether is has changed every 24h. This potentially introduces a 24h period during which the device is unable to receive notifications and messages.

## References

This limitation was already discussed in #2285, but was not the actual subject of the feature request.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.