📖 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
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
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
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
Hint
Use delete() on the document reference to remove the token.
Practice
(1/5)
1. What is the main purpose of a device token in Firebase Cloud Messaging?
easy
A. To store user passwords securely
B. To manage database access permissions
C. To track user location in real-time
D. To uniquely identify a device for sending notifications
Solution
Step 1: Understand device token role
A device token is a unique identifier assigned to each device by Firebase to target messages.
Step 2: Match purpose with options
Only To uniquely identify a device for sending notifications correctly describes the token's role in sending notifications to a specific device.
Final Answer:
To uniquely identify a device for sending notifications -> Option D
Quick Check:
Device token = unique device ID for messaging [OK]
Hint: Device tokens identify devices for messaging [OK]
Common Mistakes:
Confusing tokens with passwords
Thinking tokens track location
Assuming tokens manage database access
2. Which Firebase method is used to delete a device token?
easy
A. firebase.messaging().clearToken()
B. firebase.messaging().removeToken()
C. firebase.messaging().deleteToken()
D. firebase.messaging().revokeToken()
Solution
Step 1: Recall Firebase token deletion method
The correct method to delete a device token is deleteToken().
Step 2: Compare options with correct method
Only firebase.messaging().deleteToken() matches the exact method name deleteToken().
Final Answer:
firebase.messaging().deleteToken() -> Option C
Quick Check:
Delete token method = deleteToken() [OK]
Hint: Delete tokens with deleteToken() method [OK]