12

I use the code below to obtain a token from firebase :

const messaging = firebase.messaging();

messaging.requestPermission()
  .then(() =>{
    return firebase.messaging().getToken();
  }).then(token => {
     saveTokentoServer(user.uid, token);
  })

the problem is that i receive the same token for different users. I am not able to send out targetted messages.

Does anyone know how I can obtain different token for different users ?

I have spent two days search for answers. Why is my web application not receiving unique token ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Bubble Trouble
  • 579
  • 1
  • 8
  • 16

3 Answers3

10

Firebase Cloud Messaging tokens identify an installation of an app in a specific device. They do not identify a specific user. So in your case, if multiple users access your app in the same browser profile, they will get the same token.

I haven't tried this myself, but I think you can delete the token (typically when the user signs out). Then request permission and get the new token for the new user, as usual.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • ok.. that explains why even if I use different browsers on the same machine.. all the users still get the same token.. if I have user A login in chrome and userB log in from safari.. they are still receiving the same token .. fireabse probably identifies uniqueness based on IP it seems .. – Bubble Trouble Jul 22 '17 at 00:39
  • You're kidding, right? You are saying the user will have to to re-authorize notifications every single time he logs in whenever anyone else has logged in in the meantime? –  Mar 08 '18 at 17:25
  • Thanks for advice, Frank – Taras Vovkovych Jul 12 '19 at 17:56
2

I have an alternate solution to your problem,

You can use topics for receiving notifications,

1.) Subscribe topic for your registration_token, let the topic name unique for every user like user id.

https://iid.googleapis.com/iid/v1/<REGISTRATION_TOKEN>/rel/topics/<TOPIC_NAME>

Example:

https://iid.googleapis.com/iid/v1/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA/rel/topics/movies
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

2.) Send Notification to topic

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
  "to" : "/topics/movies",
  "priority" : "high",
  "notification" : {
    "body" : "This is a Firebase Cloud Messaging Topic Message!",
    "title" : "FCM Message",
  }
}

3.) Remove registration_id from the topic on logout

https://iid.googleapis.com/iid/v1:batchRemove
Content-Type:application/json
Authorization:key=API_KEY
{
   "to": "/topics/movies",
   "registration_tokens": ["nKctODamlM4:CKrh_PC8kIb7O..."],
}

Replace topic name "movies" with the unique user id or email etc

Rohit Dhiman
  • 2,691
  • 18
  • 33
1

Another possible solution to the problem is to add a field called userId in your data that you send as a notification. And at client side, only show notification if the userId == FirebaseAuth.user.id or similar in case you are not using FirebaseAuth.

Adi
  • 4,149
  • 4
  • 25
  • 41
  • this wont be good, becouse id user1 need to get notification and user2 is looged in and backend send message to user1, browser will recive message and not showing as usedId != 1 ... so next time user1 login he wont get message deliverd becouse FCM already send. – Matija Gluhak Nov 02 '19 at 12:58