-
Notifications
You must be signed in to change notification settings - Fork 426
Added new registration snippets for FIDs on FCM #684
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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] | ||
|
|
||
| // [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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is placed directly in the class body, which is invalid in Kotlin. It should be moved inside a function (e.g.,
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is placed directly in the class body, which is invalid in Java. It should be wrapped in a method (e.g.,
onCreateor a new helper method). Additionally, line 152 contains syntax errors: it uses Kotlin-style property access (task.exceptioninstead oftask.getException()) and is missing a trailing semicolon.