-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
base: master
Are you sure you want to change the base?
Conversation
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
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.
@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
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.
LGTM
) 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) |
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.
The new values need to be applied to the SharedPreferences after they're edited:
preferences.edit().putLong("lastRefreshDate", updatedTime) | |
preferences.edit().putLong("lastRefreshDate", updatedTime).apply() |
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
val IS_OPTIMIZE = true |
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.
Since this property is only used in this class, we can make it private
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) |
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.
Don't forget to call apply() after editing your preferences:
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.* |
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.
Avoid wildcard imports
|| lastDeviceRefresh.before(lastGlobalRefresh)) { | ||
// Get token, store into Firestore, and update cache | ||
getAndStoreRegToken() | ||
preferences.edit().putLong("lastGlobalRefreshDate", lastDeviceRefresh.time) |
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.
Don't forget to call apply:
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) |
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.
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) |
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.
preferences.edit().putLong("lastDeviceRefreshDate", Date().time) | |
preferences.edit().putLong("lastDeviceRefreshDate", Date().time).apply() |
No description provided.