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