Skip to content

FirebaseMessagingService onNewToken for non-default Firebase apps #5647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -641,19 +641,15 @@ boolean tokenNeedsRefresh(@Nullable Store.Token token) {
}

private void invokeOnTokenRefresh(String token) {
// onNewToken() is only invoked for the default app as there is no parameter to identify which
// app the token is for. We could add a new method onNewToken(FirebaseApp app, String token) or
// the like to handle multiple apps better.
if (FirebaseApp.DEFAULT_APP_NAME.equals(firebaseApp.getName())) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Invoking onNewToken for app: " + firebaseApp.getName());
}
Intent messagingIntent = new Intent(FirebaseMessagingService.ACTION_NEW_TOKEN);
messagingIntent.putExtra(FirebaseMessagingService.EXTRA_TOKEN, token);
// Previously this sent to the FIIDReceiver, which forwarded to the service.
// Send directly to service using the old FIIDReceiver mechanism to keep the change simple.
new FcmBroadcastProcessor(context).process(messagingIntent);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Invoking onNewToken for app: " + firebaseApp.getName());
}
Intent messagingIntent = new Intent(FirebaseMessagingService.ACTION_NEW_TOKEN);
messagingIntent.putExtra(FirebaseMessagingService.EXTRA_TOKEN, token);
messagingIntent.putExtra(FirebaseMessagingService.EXTRA_TOKEN_APP_NAME, firebaseApp.getName());
// Previously this sent to the FIIDReceiver, which forwarded to the service.
// Send directly to service using the old FIIDReceiver mechanism to keep the change simple.
new FcmBroadcastProcessor(context).process(messagingIntent);
}

private class AutoInit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.annotation.WorkerThread;
import com.google.android.gms.cloudmessaging.CloudMessage;
import com.google.android.gms.cloudmessaging.Rpc;
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.Constants.MessagePayloadKeys;
import com.google.firebase.messaging.Constants.MessageTypes;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class FirebaseMessagingService extends EnhancedIntentService {

static final String ACTION_NEW_TOKEN = "com.google.firebase.messaging.NEW_TOKEN";
static final String EXTRA_TOKEN = "token";
static final String EXTRA_TOKEN_APP_NAME = "firebaseAppName";

private static final int RECENTLY_RECEIVED_MESSAGE_IDS_MAX_SIZE = 10;

Expand Down Expand Up @@ -152,6 +154,19 @@ public void onSendError(@NonNull String msgId, @NonNull Exception exception) {}
@WorkerThread
public void onNewToken(@NonNull String token) {}

/**
* Called when a new token for the specified Firebase project is generated.
*
* <p>This is invoked after app install when a token is first generated, and again if the token
* changes.
*
* @param token The token used for sending messages to this application instance. This token is
* the same as the one retrieved by {@link FirebaseMessaging#getToken()}.
* @param firebaseAppName The name of the FirebaseApp instance for which the token was generated.
*/
@WorkerThread
public void onNewToken(@NonNull String token, @NonNull String firebaseAppName) {}

/** @hide */
@Override
protected Intent getStartCommandIntent(Intent originalIntent) {
Expand All @@ -167,7 +182,11 @@ public void handleIntent(Intent intent) {
if (ACTION_REMOTE_INTENT.equals(action) || ACTION_DIRECT_BOOT_REMOTE_INTENT.equals(action)) {
handleMessageIntent(intent);
} else if (ACTION_NEW_TOKEN.equals(action)) {
onNewToken(intent.getStringExtra(EXTRA_TOKEN));
String appName = intent.getStringExtra(EXTRA_TOKEN_APP_NAME);
onNewToken(intent.getStringExtra(EXTRA_TOKEN), appName);
if (appName == FirebaseApp.DEFAULT_APP_NAME) {
onNewToken(intent.getStringExtra(EXTRA_TOKEN));
}
} else {
Log.d(TAG, "Unknown intent action: " + intent.getAction());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class FirebaseMessagingServiceRoboTest {

// Extra for the token within a NEW_TOKEN event
private static final String EXTRA_TOKEN = "token";
private static final String EXTRA_TOKEN_APP_NAME = "firebaseAppName";

// blank activity
public static class MyTestActivity extends Activity {}
Expand Down Expand Up @@ -344,9 +345,10 @@ public void testSendError_messageIdServer() throws Exception {
}

@Test
public void testOnNewToken() throws Exception {
public void testOnNewTokenDefault() throws Exception {
Intent intent = new Intent(ACTION_NEW_TOKEN);
intent.putExtra(EXTRA_TOKEN, "token123");
intent.putExtra(EXTRA_TOKEN_APP_NAME, "[DEFAULT]");

ServiceStarter.getInstance().startMessagingService(context, intent);
processInternalStartService(context);
Expand All @@ -355,6 +357,19 @@ public void testOnNewToken() throws Exception {
verify(service).onNewToken("token123");
}

@Test
public void testOnNewToken() throws Exception {
Intent intent = new Intent(ACTION_NEW_TOKEN);
intent.putExtra(EXTRA_TOKEN, "token123");
intent.putExtra(EXTRA_TOKEN_APP_NAME, "customappname");

ServiceStarter.getInstance().startMessagingService(context, intent);
processInternalStartService(context);
flushTasks();

verify(service).onNewToken("token123", "customappname");
}

/**
* Test a notification message causes a notification to be posted, and none of the callbacks to be
* invoked.
Expand Down