Jeff Boker
Jeff Boker

Reputation: 909

How to send notification between Apps using FCM

I have an app that needs to send data (single integer) to another phone which has the same app. This data would pop up as a notification on the other phone.

I have been able to use FCM to send notifications to the app by using the console. However, I do that manually by sending the message. I now need to send data from the app itself to the same app on the other phone. I was not able to figure out how FCM can do this in the documentation.

I realize that I need to some how get the tokens from one phone's app and have the other app listen for any notification. The tokens are pretty large, so I don't think I should get the user to type in the tokens to listen for notifications from the other app. How can I achieve my goal?

Upvotes: 3

Views: 275

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61351

Since this is a learning experience, seamless experience/user friendliness is probably not much of a concern. If you don't have a central server and are not planning to have one, enable the standard "Share" interface on the token, and let users send it around by any supported means of human-to-human communication - SMS, e-mail, WhatsApp, anything.

On the target device, let them paste it into the app UI.

The logic to invoke the standard "Share..." UI goes:

String TheToken; //Comes from the FCM registration callback...
startActivity(
    Intent.createChooser(
    new Intent(Intent.ACTION_SEND)
        .setType("text/plain")
        .putExtra(Intent.EXTRA_TEXT, TheToken), ""));

This snippet assumes it's in an Activity method, i. e. the current this object is a Context instance.

In a real life application, you'd probably want a central server.

Another avenue would be - there are some options that rely on physical proximity. Read up on Network Service Discovery (NSD); that's a means for an application to find other instances of the same application running on devices on the same Wi-Fi. Internally, it's the Zeroconf/Bonjour protocol, facilitated by DNS multicast. This kind of logic is more involved; you'd have to spin up your own network server on a custom port.

Other options for communication with nearby devices include Bluetooth PAN and Near Field Communication (NFC). I have no experience with either of those, sorry. I just know they exist :)

Upvotes: 1

Related Questions