Added new registration snippets for FIDs on FCM#684
Conversation
| // [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] |
There was a problem hiding this comment.
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.
| // [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] |
There was a problem hiding this comment.
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.
| // [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] |
There was a problem hiding this comment.
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.
| // [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] |
There was a problem hiding this comment.
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).
Added new registration snippets for FIDs on FCM