Android Question Firebase Messaging: Send message to single device

yiankos1

Well-Known Member
Licensed User
Longtime User
I want to add at my project a chat but only user to user. Just two users, not subscribe to a topic. As @KMatle advises us:
Please take it as a example how to communicate between devices via FCM without a server in the middle. Do not publish a similar app to the PlayStore. It is good for SOME devices as each device will send it's identification data to all devices an vice versa. When the number of apps grow, there is a huge communication traffic (1 Mio devices will send a message to 999.999 devices and will retrieve 999.999 messages from all the others):)
So my question is how can achieve this? Is it better to create a new topic for both users or exists another to way to send a message to a single device. I think tokens changes randomly how can we catch this?
For example if i want to send a message to a user and token has changes i think user never get this message.
 

tigrot

Well-Known Member
Licensed User
Longtime User
I use to store the token of each user in a DB (phone # along with the token which Firebase assigns to each user). When I need to comunicate with each user, I get the key by phone # and send the message directly to the user. You can handle a status on the DB as well. Not ready/ready is generally enough. So every user must update the DB to keep informations up to date
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
I use to store the token of each user in a DB (phone # along with the token which Firebase assigns to each user). When I need to comunicate with each user, I get the key by phone # and send the message directly to the user. You can handle a status on the DB as well. Not ready/ready is generally enough. So every user must update the DB to keep informations up to date

In order to keep this DB up to date with new tokens, app needs to run a service (even it is in background) every couple of secs. Is this the right way? Moreover i would like to know from example which parts of code should i keep and use it in order to send to a single TOKEN-DEVICE(which i would get from a db).
Thank you in advance.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In order to keep this DB up to date with new tokens, app needs to run a service (even it is in background) every couple of secs. Is this the right way
No.

Get the token when the app starts with FirebaseMessaging.Token and send it to your server. It will not change while your app is running.

Moreover i would like to know from example which parts of code should i keep and use it in order to send to a single TOKEN-DEVICE
https://www.b4x.com/android/forum/t...-cloud-messaging-fcm.67716/page-3#post-433583
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
As Erel said, you'll need a server to store all the tokens. I use a cheap hosted solution with php & MySql. Even more important: You need to handle users which have to register/login (security) like in this forum. For sure you need to store other informations, too depending on your app.
 
Upvote 0
Top