Reputation: 49
I need to set the device token to the UID when a user logs in. Is this possible? If yes How?
Upvotes: 1
Views: 2182
Reputation: 598668
There is no way to set the device token for Firebase Cloud Messaging yourself. It would actually be an anti-pattern to set a user's ID, since the same user may end up using the app on different devices.
If you're trying to send a message to a user (even when that user is on multiple devices), you'll need to associate the device tokens with the UID yourself. I know of a few ways to do this:
One way to do that is to combine the tokens into a device group, and then send a message to that device group.
Another way would be to store the device tokens for each UID yourself, for example in the Firebase Database or Cloud Firestore, and the loop over them to send a message to all device tokens for a user.
The third approach does not require you to manage tokens at all. Instead: create a topic for each user, and have the user subscribe to their own topic. I described this approach in Sending notifications between Android devices with Firebase Database and Cloud Messaging. Note that anyone who knows a topic ID can subscribe to that topic, so you should only use this approach for public (but targeted) communication.
Upvotes: 3