Skip to content

Commit

Permalink
add snippets to demonstrate fcm registration tokens best practices (#426
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
thatfiredev authored Feb 13, 2023
1 parent 067d83a commit 1e4d075
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions messaging/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
Expand All @@ -30,8 +31,14 @@ dependencies {
// for Google Analytics. This is recommended, but not required.
implementation 'com.google.firebase:firebase-analytics:21.2.0'

// Used to store FCM Registration Token.
// This is recommended, but not required.
// See: https://firebase.google.com/docs/cloud-messaging/manage-tokens
implementation 'com.google.firebase:firebase-firestore-ktx:24.4.3'

implementation "com.google.android.gms:play-services-auth:20.4.1"
implementation 'androidx.work:work-runtime-ktx:2.7.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
}

apply plugin: 'com.google.gms.google-services'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.google.firebase.example.messaging.kotlin

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
Expand All @@ -10,11 +11,19 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.google.firebase.Timestamp
import com.google.firebase.example.messaging.MainActivity
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.messaging.ktx.remoteMessage
import java.util.Calendar
import java.util.Date
import java.util.concurrent.atomic.AtomicInteger
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await

class MainActivity : AppCompatActivity() {

Expand Down Expand Up @@ -130,4 +139,20 @@ class MainActivity : AppCompatActivity() {
}
// [END ask_post_notifications]

// [START get_store_token]
private suspend fun getAndStoreRegToken(): String {
val 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
}
// [END get_store_token]

}
44 changes: 44 additions & 0 deletions messaging/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'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.
*/
// [START refresh_date_scheduled_function]
exports.scheduledFunction = functions.pubsub.schedule('0 0 1 * *').onRun((context) => {
admin.firestore().doc('refresh/refreshDate').set({ lastRefreshDate : Date.now() });
});
// [END refresh_date_scheduled_function]

/**
* Scheduled function that runs once a day. It retrieves all stale tokens then
* unsubscribes them from 'topic1' then deletes them.
*
* Note: weather is an example topic here. It is up to the developer to unsubscribe
* all topics the token is subscribed to.
*/
// [START remove_stale_tokens]
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, 'weather');

const deletePromises = [];
for (const staleTokenDoc of staleTokensResult.docs) {
deletePromises.push(staleTokenDoc.ref.delete());
}
await Promise.all(deletePromises);
});
// [END remove_stale_tokens]

0 comments on commit 1e4d075

Please sign in to comment.