Skip to content
Closed
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 @@ -141,4 +141,38 @@ private void askNotificationPermission() {
}
}
// [END ask_post_notifications]

// [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]
Comment on lines +145 to +157
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]
}


// [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]
Comment on lines +159 to +177
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.

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,36 @@ class MainActivity : AppCompatActivity() {
return token
}
// [END get_store_token]

// [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]
Comment on lines +155 to +166
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]
}


// 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]
Comment on lines +168 to +185
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).

}
Loading