Android Example Firebase Messaging: Chat app without server

When you register to FCM your app will receive a FCM token. Another app can send a message to this token. Probem here: How do I know the token of another device/user? Usually you have a server where the app stores it's own token with a userid and retrieves the other devices token via some logic.

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

How it works:

- a unique nickname (Name plus IP address) will be created at the start
- all devices register to a topic "all" to exchange the own token
- with a timer (change the if needed) the app sends a fcm message to this topic including the nickname and the token
- every device receives the nickname and the token and stores it in a map.
- this list will be added to a spinner (so you can select who you want to send a message to)
- type in a message and press send. This will send a message to the selected receiver (=token)

Please

- copy your google-services.json to the main folder
- add you app's FCM Server Key (aka "Server Key") in FirebaseMessaging service module
- change the nickname as you need (must be unique)

Known "issues":

- at the first start sometimes the google services may not register you app correctly (you'll see a message then). Just restart the app and everything is fine.

Optimizations

- one device could be the server
- only this device subscribes to ONE topic (not the others)
- the other devices can send messages to this topic (name & token) without having subscribed to it
- the other devices do not need any ip address (they just send to a topic)
- after that the devices can send messages to this topic like
- retrieve all device's names and tokens
- login/logoff
- the central device (server) then sends back a message by the token (= to the asking device)
- add encryption if needed (take care - only 4 KB length per message)
- only google knows who is communicating with each other (no ip addresses known, no direct communication between ip-address a and b)
- nothing will be stored on a device (except if you want to)
- when you delete the app, no data is kept on the device
 

Attachments

  • FCM_Show.zip
    33.5 KB · Views: 1,286

VictorTandil

Member
Licensed User
A question. Is there any tutorial on how to create a server with B4J to handle incoming / outgoing messages?
 

tigrot

Well-Known Member
Licensed User
Longtime User
You must have at least a public accessible DB server to store user ID and its token. When you need to comunicate you access the DB and find out the token to which address the message. I personally use phone number as ID. If have sorted out to use Firebase Realtime Database, but it's a little expensive.
 

Alexander Stolte

Expert
Licensed User
By the way, it's not possible to handle incoming messages in B4J using Firebase. You can send only.

My Idea is, if i write a new post in the Chat, i send my tocken to the database, the b4j application looks in there and send the message with the tocken to firbase, and firebase send the message to the other devices.
 

tigrot

Well-Known Member
Licensed User
Longtime User
Where do you store DB data? What kind of DB? On the same server as B4J (or in the same network) is ok. It's a feasible solution. Or you can create a topic on Firebase Messaging system. In this way any subscribed device will receive the published message! No work to do on B4J.
 

VictorTandil

Member
Licensed User
I like Alexander's idea. The server (B4J) would be hosted on a PC and wait for the arrival of the messages using "HttpUtils2" and from there it will be forwarded to the destination. A "DB" should be created on the server where messages and "Tokens" are stored. Although in this case, I do not know how much needed Firebase. The good thing about this is that in addition to text you can send files as photos or audio.-

Am I missing something?
 

Alexander Stolte

Expert
Licensed User
I have expanded my idea a little bit. My idea is, the user subscribe a channel, send a request to the database(over RDC to my jRDC) write his token to this channel. If users writing a post in this channel, get all those, who have subscribed to the channel the message in this room. So that the messages are still there, they are written as in whatsapp in their own local database and are still available days later.

The "RealTimeDatabase" was the thing what i need, but you can not have everything. And send a message over firebase push notification is free, so i save storage in the datacenter. The disadvantage is, subscribed a new user this channel, the messages before, are done for the new user. And double upload is not resource-efficient.

The database is important for me, because the real time database in b4a is not supported.
 

JEG

Member
Licensed User
Longtime User
I have loaded FMC_Show. Made changes. Compiles without any errors but when loading to device reports Parse Error. "There is a problem parsing this package"
Any Ideas?
 
Top