sample implementation of registration tokens for FCM#1453
Open
sample implementation of registration tokens for FCM#1453
Conversation
thatfiredev
reviewed
Jan 30, 2023
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
...ng/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt
Outdated
Show resolved
Hide resolved
thatfiredev
reviewed
Jan 31, 2023
Member
thatfiredev
left a comment
There was a problem hiding this comment.
@andreaowu CI is failing here due to trailing commas which was only introduced in Kotlin recently. We are running an old version of ktlint. I'll put up a PR to fix it
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
thatfiredev
added a commit
to firebase/snippets-android
that referenced
this pull request
Feb 13, 2023
) Copying the snippets from firebase/quickstart-android#1453 to this repo so that they can be added to the docs. Having these snippets on the other repo makes it harder to maintain the samples. Having them on this repo instead should be safer.
thatfiredev
reviewed
Feb 16, 2023
| // sync device cache time with Firestore just in case | ||
| val document = Firebase.firestore.collection("refresh").document("refreshDate").get().await() | ||
| val updatedTime = (document.data!!["lastRefreshDate"] as Timestamp).seconds * 1000 | ||
| preferences.edit().putLong("lastRefreshDate", updatedTime) |
Member
There was a problem hiding this comment.
The new values need to be applied to the SharedPreferences after they're edited:
Suggested change
| preferences.edit().putLong("lastRefreshDate", updatedTime) | |
| preferences.edit().putLong("lastRefreshDate", updatedTime).apply() |
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| val IS_OPTIMIZE = true |
Member
There was a problem hiding this comment.
Since this property is only used in this class, we can make it private
Suggested change
| val IS_OPTIMIZE = true | |
| private val IS_OPTIMIZE = true |
|
|
||
| // As an optimization, store today’s date in Android cache | ||
| val preferences = this.getSharedPreferences("default", Context.MODE_PRIVATE) | ||
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time) |
Member
There was a problem hiding this comment.
Don't forget to call apply() after editing your preferences:
Suggested change
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time) | |
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time).apply() |
| import com.google.firebase.messaging.FirebaseMessagingService | ||
| import com.google.firebase.messaging.RemoteMessage | ||
| import com.google.firebase.quickstart.fcm.R | ||
| import java.util.* |
| || lastDeviceRefresh.before(lastGlobalRefresh)) { | ||
| // Get token, store into Firestore, and update cache | ||
| getAndStoreRegToken() | ||
| preferences.edit().putLong("lastGlobalRefreshDate", lastDeviceRefresh.time) |
Member
There was a problem hiding this comment.
Don't forget to call apply:
Suggested change
| preferences.edit().putLong("lastGlobalRefreshDate", lastDeviceRefresh.time) | |
| preferences.edit().putLong("lastGlobalRefreshDate", lastDeviceRefresh.time).apply() |
| if (lastGlobalRefreshLong == -1L || today.after(c.time)) { | ||
| val document = Firebase.firestore.collection("refresh").document("refreshDate").get().await() | ||
| val updatedTime = (document.data!!["lastRefreshDate"] as Timestamp).seconds * 1000 | ||
| preferences.edit().putLong("lastGlobalRefreshDate", updatedTime) |
Member
There was a problem hiding this comment.
Suggested change
| preferences.edit().putLong("lastGlobalRefreshDate", updatedTime) | |
| preferences.edit().putLong("lastGlobalRefreshDate", updatedTime).apply() |
| // As an optimization, store today’s date in Android cache | ||
| if (IS_OPTIMIZE) { | ||
| val preferences = this.getSharedPreferences("default", Context.MODE_PRIVATE) | ||
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time) |
Member
There was a problem hiding this comment.
Suggested change
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time) | |
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time).apply() |
Anaxdi30
approved these changes
Jul 4, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.