-
Notifications
You must be signed in to change notification settings - Fork 7.4k
sample implementation of registration tokens for FCM #1453
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
Open
andreaowu
wants to merge
15
commits into
master
Choose a base branch
from
reg-token-sample
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fca8b50
sample implementation of registration tokens for FCM
andreaowu 32c4b65
made changes according to comments
andreaowu 88f4bf5
Merge remote-tracking branch 'origin/master' into reg-token-sample
andreaowu 4ef6bba
resolve comments and remove logs
andreaowu 61a1767
fix scope
andreaowu 5692eb5
remove duped code and simplify
andreaowu 1ff3190
added code to remove expired tokens from topics
andreaowu cf9f113
fix functions code
andreaowu 7f876a4
small code cleanup
andreaowu baa8132
add snippets
andreaowu defd24c
have both optimized and non-optimized versions
andreaowu 248b3e6
use flag for whether to use optimized version or not
andreaowu d9cd34b
update code
andreaowu 8057452
fix: add tag
HYACCCINT 0b77e46
Merge pull request #1512 from firebase/cynthia-sample-tag
HYACCCINT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ package com.google.firebase.quickstart.fcm.kotlin | |||||
import android.Manifest | ||||||
import android.app.NotificationChannel | ||||||
import android.app.NotificationManager | ||||||
import android.content.Context | ||||||
import android.content.pm.PackageManager | ||||||
import android.os.Build | ||||||
import android.os.Bundle | ||||||
|
@@ -11,11 +12,18 @@ import android.widget.Toast | |||||
import androidx.activity.result.contract.ActivityResultContracts | ||||||
import androidx.appcompat.app.AppCompatActivity | ||||||
import androidx.core.content.ContextCompat | ||||||
import com.google.android.gms.tasks.OnCompleteListener | ||||||
import androidx.lifecycle.lifecycleScope | ||||||
import com.google.firebase.Timestamp | ||||||
import com.google.firebase.firestore.FieldValue | ||||||
import com.google.firebase.firestore.ktx.firestore | ||||||
import com.google.firebase.ktx.Firebase | ||||||
import com.google.firebase.messaging.ktx.messaging | ||||||
import com.google.firebase.quickstart.fcm.R | ||||||
import com.google.firebase.quickstart.fcm.databinding.ActivityMainBinding | ||||||
import kotlinx.coroutines.launch | ||||||
import kotlinx.coroutines.tasks.await | ||||||
import java.util.Calendar | ||||||
import java.util.Date | ||||||
|
||||||
class MainActivity : AppCompatActivity() { | ||||||
|
||||||
|
@@ -80,25 +88,40 @@ class MainActivity : AppCompatActivity() { | |||||
binding.logTokenButton.setOnClickListener { | ||||||
// Get token | ||||||
// [START log_reg_token] | ||||||
Firebase.messaging.getToken().addOnCompleteListener(OnCompleteListener { task -> | ||||||
if (!task.isSuccessful) { | ||||||
Log.w(TAG, "Fetching FCM registration token failed", task.exception) | ||||||
return@OnCompleteListener | ||||||
} | ||||||
|
||||||
lifecycleScope.launch { | ||||||
// Get new FCM registration token | ||||||
val token = task.result | ||||||
val token = getAndStoreRegToken() | ||||||
|
||||||
// Log and toast | ||||||
val msg = getString(R.string.msg_token_fmt, token) | ||||||
Log.d(TAG, msg) | ||||||
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() | ||||||
}) | ||||||
} | ||||||
// [END log_reg_token] | ||||||
} | ||||||
|
||||||
Toast.makeText(this, "See README for setup instructions", Toast.LENGTH_SHORT).show() | ||||||
askNotificationPermission() | ||||||
|
||||||
// In the app’s first Activity | ||||||
val preferences = this.getPreferences(Context.MODE_PRIVATE) | ||||||
val lastRefreshLong = preferences.getLong("lastRefreshDate", -1) | ||||||
lifecycleScope.launch { | ||||||
val c = Calendar.getInstance().apply { | ||||||
time = if (lastRefreshLong == -1L) Date() else Date(lastRefreshLong) | ||||||
add(Calendar.DATE, 30) | ||||||
} | ||||||
|
||||||
val today = Date() | ||||||
if (today.after(c.time) || lastRefreshLong == -1L) { | ||||||
// get token and store into Firestore | ||||||
getAndStoreRegToken() | ||||||
// 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 commentThe 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
|
||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private fun askNotificationPermission() { | ||||||
|
@@ -115,6 +138,20 @@ class MainActivity : AppCompatActivity() { | |||||
} | ||||||
} | ||||||
|
||||||
private suspend fun getAndStoreRegToken(): String { | ||||||
var token = Firebase.messaging.token.await() | ||||||
// Add token and timestamp to Firestore for this user | ||||||
val deviceToken = hashMapOf( | ||||||
"token" to token, | ||||||
"timestamp" to FieldValue.serverTimestamp(), | ||||||
) | ||||||
|
||||||
// Get user ID from Firebase Auth or your own server | ||||||
Firebase.firestore.collection("fcmTokens").document("myuserid") | ||||||
.set(deviceToken).await() | ||||||
return token | ||||||
} | ||||||
|
||||||
companion object { | ||||||
|
||||||
private const val TAG = "MainActivity" | ||||||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const functions = require('firebase-functions'); | ||
const admin = require('firebase-admin'); | ||
|
||
admin.initializeApp(); | ||
|
||
const EXPIRATION_TIME = 1000 * 60 * 60 * 24 * 30; // 30 days | ||
|
||
/** | ||
* Scheduled function that runs once a month. It updates the last refresh date for | ||
* tokens so that a client can refresh the token if the last time it did so was | ||
* before the refresh date. | ||
*/ | ||
|
||
exports.scheduledFunction = functions.pubsub.schedule('0 0 1 * *').onRun((context) => { | ||
admin.firestore().doc('refresh/refreshDate').set({ lastRefreshDate : Date.now() }); | ||
}); | ||
|
||
/** | ||
* Scheduled function that runs once a day. It retrieves all stale tokens then | ||
* unsubscribes them from 'topic1' then deletes them. | ||
* | ||
* Note: topic1 is an example topic here. It is up to the developer to unsubscribe | ||
* all topics the token is subscribed to. | ||
*/ | ||
exports.pruneTokens = functions.pubsub.schedule('every 24 hours').onRun(async (context) => { | ||
const staleTokensResult = await admin.firestore().collection('fcmTokens') | ||
.where("timestamp", "<", Date.now() - EXPIRATION_TIME) | ||
.get(); | ||
|
||
const staleTokens = staleTokensResult.docs.map(staleTokenDoc => staleTokenDoc.id); | ||
|
||
await admin.messaging().unsubscribeFromTopic(staleTokens, 'topic1'); | ||
|
||
const deletePromises = []; | ||
for (const staleTokenDoc of staleTokensResult.docs) { | ||
deletePromises.push(staleTokenDoc.ref.delete()); | ||
} | ||
await Promise.all(deletePromises); | ||
}); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.