Uh oh!
There was an error while loading. Please reload this page.
sample implementation of registration tokens for FCM - #1453
Conversation
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
) 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.
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.
Since this property is only used in this class, we can make it private
| valIS_OPTIMIZE=true | |
| privatevalIS_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.
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.* |
| || 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.
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.
| 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.
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time) | |
| preferences.edit().putLong("lastDeviceRefreshDate", Date().time).apply() |
No description provided.