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:
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

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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…