0
0
Firebasecloud~10 mins

Device token management in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Device token management
App starts
Request device token
Receive token from Firebase
Store token in database
Use token to send notifications
Token refresh event?
YesGet new token
Update token in database
End
The app requests a device token from Firebase, stores it, uses it for notifications, and updates it when refreshed.
Execution Sample
Firebase
firebase.messaging().getToken()
  .then(token => {
    saveTokenToDatabase(token);
  });
This code gets the device token from Firebase and saves it to the database.
Process Table
StepActionFirebase ResponseDatabase ActionResult
1App requests device tokenToken requestedNo action yetWaiting for token
2Firebase returns tokenToken: abc123xyzNo action yetToken received
3Save token to databaseN/AStore token abc123xyzToken stored
4Send notification using tokenN/AUse token abc123xyzNotification sent
5Token refresh event occursNew token requestedNo action yetWaiting for new token
6Firebase returns new tokenToken: def456uvwNo action yetNew token received
7Update token in databaseN/AReplace old token with def456uvwToken updated
8No further refreshN/ANo actionProcess ends
💡 No token refresh event, process ends
Status Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 7Final
deviceTokennullabc123xyzabc123xyzdef456uvwdef456uvwdef456uvw
databaseTokennullnullabc123xyzabc123xyzdef456uvwdef456uvw
Key Moments - 2 Insights
Why do we need to update the token in the database after a refresh?
Because the token changes (see Step 6 and 7 in execution_table), and the database must have the current token to send notifications correctly.
What happens if the app does not request a new token after refresh?
Notifications will fail because the old token becomes invalid (refer to Step 5 and 6 where a new token is needed).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the device token after Step 3?
Anull
Babc123xyz
Cdef456uvw
DToken not received yet
💡 Hint
Check the 'deviceToken' value after Step 3 in variable_tracker.
At which step does the token get updated in the database?
AStep 7
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Database Action' column in execution_table for token update.
If the token refresh event never occurs, what happens to the token in the database?
AIt changes automatically
BIt gets deleted
CIt stays the same
DIt causes an error
💡 Hint
Refer to the exit_note and final variable values in variable_tracker.
Concept Snapshot
Device token management in Firebase:
- Request token with getToken()
- Store token in database
- Use token to send notifications
- Listen for token refresh
- Update database with new token
- Keep tokens current for reliable messaging
Full Transcript
Device token management in Firebase involves the app requesting a unique token from Firebase Messaging. This token identifies the device for sending notifications. Once received, the token is saved in a database. The app uses this token to send notifications to the device. When Firebase refreshes the token, the app must get the new token and update it in the database to keep notifications working. If the token is not updated, notifications will fail. This process ensures reliable delivery of messages to the device.