Skip to content
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

sample implementation of registration tokens for FCM #1453

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

andreaowu
Copy link

No description provided.

Copy link
Member

@thatfiredev thatfiredev left a comment

Choose a reason for hiding this comment

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

@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

Copy link
Member

@thatfiredev thatfiredev left a comment

Choose a reason for hiding this comment

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

LGTM

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.
// 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)
Copy link
Member

Choose a reason for hiding this comment

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

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
Copy link
Member

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

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.*
Copy link
Member

Choose a reason for hiding this comment

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

Avoid wildcard imports

|| lastDeviceRefresh.before(lastGlobalRefresh)) {
// Get token, store into Firestore, and update cache
getAndStoreRegToken()
preferences.edit().putLong("lastGlobalRefreshDate", lastDeviceRefresh.time)
Copy link
Member

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
preferences.edit().putLong("lastDeviceRefreshDate", Date().time)
preferences.edit().putLong("lastDeviceRefreshDate", Date().time).apply()

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.

None yet

3 participants