0
0
Firebasecloud~30 mins

Device token management in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Device Token Management with Firebase
📖 Scenario: You are building a mobile app that sends notifications to users. To do this, you need to manage device tokens securely and efficiently using Firebase Cloud Messaging (FCM).
🎯 Goal: Create a simple Firebase Cloud Function to store device tokens in Firestore, update them when needed, and delete tokens when users log out.
📋 What You'll Learn
Create a Firestore collection called deviceTokens to store tokens.
Write a Cloud Function to add a new device token to Firestore.
Write a Cloud Function to update an existing device token.
Write a Cloud Function to delete a device token when a user logs out.
💡 Why This Matters
🌍 Real World
Managing device tokens is essential for sending push notifications to users in mobile apps securely and efficiently.
💼 Career
This project teaches skills used by cloud engineers and backend developers working with Firebase to handle user device data and notifications.
Progress0 / 4 steps
1
Create Firestore collection and add device token
Create a Cloud Function called addDeviceToken that takes userId and token as parameters and adds a document to the deviceTokens collection with userId as the document ID and token as a field.
Firebase
Need a hint?

Use functions.https.onCall to create the function. Use admin.firestore().collection('deviceTokens').doc(userId).set({ token: token }) to add the token.

2
Add update token configuration
Create a variable called updateOptions with the value { merge: true } to allow updating existing tokens without overwriting the whole document.
Firebase
Need a hint?

Define const updateOptions = { merge: true } to use later for updating tokens.

3
Create updateDeviceToken function
Create a Cloud Function called updateDeviceToken that takes userId and newToken as parameters and updates the token field in the deviceTokens collection using updateOptions to merge changes.
Firebase
Need a hint?

Use set with updateOptions to update the token without overwriting the whole document.

4
Create deleteDeviceToken function
Create a Cloud Function called deleteDeviceToken that takes userId as a parameter and deletes the corresponding document from the deviceTokens collection.
Firebase
Need a hint?

Use delete() on the document reference to remove the token.