Skip to content

Added new registration snippets for FIDs on FCM#684

Closed
s-chandels wants to merge 1 commit into
firebase:masterfrom
s-chandels:master
Closed

Added new registration snippets for FIDs on FCM#684
s-chandels wants to merge 1 commit into
firebase:masterfrom
s-chandels:master

Conversation

@s-chandels
Copy link
Copy Markdown

@s-chandels s-chandels commented May 21, 2026

Added new registration snippets for FIDs on FCM

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds manual Firebase Cloud Messaging (FCM) registration logic and an onRegistered callback to both the Java and Kotlin implementations of MainActivity.

Comment on lines +145 to +157
// [START register_fid]
// Trigger manual registration if auto-initialization is turned off.
// Consider calling this every time the app starts to guarantee sync status.
FirebaseMessaging.getInstance().register()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
// Registration failed. Consider retrying the registration with exponential backoff.
Log.w(TAG, "Failed to register with Firebase Cloud Messaging", task.exception)
}
// Success! The Firebase Installation ID can be used to target messages to this app
// instance and will be delivered asynchronously to your onRegistered() callback.
});
// [END register_fid]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This code is placed directly in the class body, which is invalid in Java. It should be wrapped in a method (e.g., onCreate or a new helper method). Additionally, line 152 contains syntax errors: it uses Kotlin-style property access (task.exception instead of task.getException()) and is missing a trailing semicolon.

Suggested change
// [START register_fid]
// Trigger manual registration if auto-initialization is turned off.
// Consider calling this every time the app starts to guarantee sync status.
FirebaseMessaging.getInstance().register()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
// Registration failed. Consider retrying the registration with exponential backoff.
Log.w(TAG, "Failed to register with Firebase Cloud Messaging", task.exception)
}
// Success! The Firebase Installation ID can be used to target messages to this app
// instance and will be delivered asynchronously to your onRegistered() callback.
});
// [END register_fid]
public void registerFid() {
// [START register_fid]
// Trigger manual registration if auto-initialization is turned off.
// Consider calling this every time the app starts to guarantee sync status.
FirebaseMessaging.getInstance().register()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
// Registration failed. Consider retrying the registration with exponential backoff.
Log.w(TAG, "Failed to register with Firebase Cloud Messaging", task.getException());
}
// Success! The Firebase Installation ID can be used to target messages to this app
// instance and will be delivered asynchronously to your onRegistered() callback.
});
// [END register_fid]
}

Comment on lines +159 to +177
// [START on_fid_registered]
/**
* There are three scenarios when `onRegistered` is called:
* 1) Every time a manual `register()` call finishes successfully
* 2) Whenever the FID is changed and the app is re-registered with FCM via the new FID
* 3) Automatically on app startup or routine sync when auto-initialization is enabled.
* Under #2, there are three scenarios when the existing FID is changed:
* A) App is restored to a new device
* B) User uninstalls/reinstalls the app
* C) User clears app data
*/
@Override
public void onRegistered(@NonNull String installationId) {
Log.d(TAG, "Registered installation ID: " + installationId);

// Send the Firebase Installation ID to your app server.
sendRegistrationToServer(installationId);
}
// [END on_fid_registered]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The onRegistered callback is part of the FirebaseMessagingService API and should be moved to MyFirebaseMessagingService.java. In its current location in MainActivity, the @Override annotation is invalid because AppCompatActivity does not define this method. Furthermore, sendRegistrationToServer is not defined in this class, which will cause a compilation error.

Comment on lines +155 to +166
// [START register_fid]
// Trigger manual registration if auto-initialization is turned off.
// Consider calling this every time the app starts to guarantee sync status.
FirebaseMessaging.getInstance().register()
.addOnCompleteListener(this) { task ->
if (!task.isSuccessful()) {
Log.w(TAG, "Failed to register with Firebase Cloud Messaging", task.exception)
}
// Success! The Firebase Installation ID can be used to target messages to this app
// instance and will be delivered asynchronously to your onRegistered() callback.
}
// [END register_fid]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This code is placed directly in the class body, which is invalid in Kotlin. It should be moved inside a function (e.g., onCreate or a dedicated method). Also, using the Firebase.messaging KTX property is more idiomatic in Kotlin.

Suggested change
// [START register_fid]
// Trigger manual registration if auto-initialization is turned off.
// Consider calling this every time the app starts to guarantee sync status.
FirebaseMessaging.getInstance().register()
.addOnCompleteListener(this) { task ->
if (!task.isSuccessful()) {
Log.w(TAG, "Failed to register with Firebase Cloud Messaging", task.exception)
}
// Success! The Firebase Installation ID can be used to target messages to this app
// instance and will be delivered asynchronously to your onRegistered() callback.
}
// [END register_fid]
fun registerFid() {
// [START register_fid]
// Trigger manual registration if auto-initialization is turned off.
// Consider calling this every time the app starts to guarantee sync status.
Firebase.messaging.register()
.addOnCompleteListener(this) { task ->
if (!task.isSuccessful) {
Log.w(TAG, "Failed to register with Firebase Cloud Messaging", task.exception)
}
// Success! The Firebase Installation ID can be used to target messages to this app
// instance and will be delivered asynchronously to your onRegistered() callback.
}
// [END register_fid]
}

Comment on lines +168 to +185
// START [on_fid_registered]
/**
* There are three scenarios when `onRegistered` is called:
* 1) Every time a manual `register()` call finishes successfully
* 2) Whenever the FID is changed and the app is re-registered with FCM via the new FID.
* 3) Automatically on app startup or routine sync when auto-initialization is enabled.
* Under #2, there are three scenarios when the existing FID is changed:
* A) App is restored to a new device
* B) User uninstalls/reinstalls the app
* C) User clears app data
*/
override fun onRegistered(installationId: String) {
Log.d(TAG, "Registered installation ID: $installationId")

// Send the Firebase Installation ID to your app server.
sendRegistrationToServer(installationId)
}
// [END on_fid_registered]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The onRegistered function belongs in FirebaseMessagingService and should be moved to MyFirebaseMessagingService.kt. In MainActivity, the override keyword will cause a compilation error as it does not match any member in the base class. Additionally, there is a typo in the opening tag on line 168 (START [ should be [START).

@s-chandels s-chandels closed this May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant