Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to register an FCM token for a user

I have some doubts about implementing Firebase Cloud Messaging in my app.

My app is a chat similar to whatsapp and facebook messenger. Right now every time someone logs in I register the token and relate it to the user. So if someone wants to chat to an user the app searches the Db for the user ID and there I have his token.

I'm able to register the device token but I'm not sure when should I do that.

In my chat users register through a mail/password signup and is possible that an user could login in other device.

For example: User Frehley uses a Galaxy 8 and is able to chat and receive notifications in his device using the token. But lets say that he loges in another device. I need to register the token again and relate it to him right?

So, maybe I'm wrong but the best moment to register the token is every time the user logs in and replace it in the DB. Right?

Now, lets say two users (Frehley and Stanley) uses the same Galaxy 8 to login, the token will be diferent everytime the login or it is the same for every single device?

like image 323
Ace Avatar asked Jul 29 '17 07:07

Ace


People also ask

Why do we need FCM token?

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. This is the token that you must include in targeted send requests from the API, or add to topic subscriptions for targeting topics.

What is FCM registration?

Firebase Cloud Messaging (FCM) is a messaging solution that lets you reliably send messages at no cost to both Android & iOS devices. Using FCM, you can send data payloads (via a message) to a device for a specific application. Each message can transfer a payload of up to 4KB to a client.


1 Answers

This all depends on the use-case:

  • If the same user is logged in to two devices, do you want them to receive the notification on both devices? If so, you'll need to store multiple device tokens per user and send the notification to each token for the targeted user. You could also use a device group to identify the devices for a single user. From those docs:

    With device group messaging, you can send a single message to multiple instances of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user

  • If you want the user to only receive the notification on the device that they were last using, you'll have to associate a single token with each user and overwrite that when they start the app on a device. Not that this is not necessarily when they log in, but more likely when the app detects that they're authenticated.

  • If a different user uses the same app on the same device (using the same device profile), you probably want to stop sending to the device token. The most robust way to do this is to remove the association between the previous user and the token, but that may require that you keep a mapping from token to UID. An easier way is to delete the device token when a user signs out. Then the new user signs in, they will then get a new token.

like image 77
Frank van Puffelen Avatar answered Sep 19 '22 16:09

Frank van Puffelen