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 ?
When the time comes to send a message, the token is read from the datastore and a data payload is sent via FCM to the specific device with the token assigned. A messaging token is unique per device and application.
For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.
They are often used interchangeably and neither are really "official" terms. For the most part, both terms refer to your APNS/FCM token, however device token has historically been used for other things like unique device identifiers.
This is a code behavior and documentation question. There is no information about the following situation. FCM token for an app may change due to various reasons that are specified in the Firebase documentation. It is also documented that clients should have a monthly job to sync the new token with the service.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With