2

There is a ton of documentation explaining how to send messages from the server to the Android device, but next to nothing available for how to send it from the Android device to the server.

We are working on making an app where the users can receive notifications from one another in certain circumstances. I am receiving messages just fine and my code works to parse them when received. Sending messages, on the other hand, is not working.

The code I am attempting to work with is:

public static void sendNotificationToUser(String userId, final String message){
        FirebaseMessaging messaging = FirebaseMessaging.getInstance();

        Bundle someBundle = new Bundle();
        someBundle.putString("STRING_KEY_HERE", "STRING_VALUE_HERE");
        //I have tried excluding the @gcm as well, no luck.
        String str = "user_" + userId + "@gcm.googleapis.com";

        RemoteMessage.Builder myBuilder = new RemoteMessage.Builder(str);
        Map<String, String> aMap = new HashMap<>();

        aMap.put("DATA_KEY_1", "DATA_VALUE_1");
        myBuilder.setData(aMap);
        myBuilder.addData("ADD_DATA1", "ADD_DATA11");
        myBuilder.setMessageId("SOME_MESSAGE_ID123");
        myBuilder.setTtl(0); //Send immediately
        myBuilder.setMessageType("SOME_MESSAGE_TYPE");
        myBuilder.setCollapseKey("SOME_COLLAPSE_KEY");

        //Can't do this, private access, how do I instantiate this?
        RemoteMessage.Notification notification = new RemoteMessage.Notification();
        RemoteMessage remoteMessage = myBuilder.build();

        messaging.send(remoteMessage);
    }

The problem here is that once this fires off, nothing ever happens. Specifically, what I am trying to do is send a message as per subscribed topics. So, if I were subscribed to android_tag, I want to send this message to the android_tag, which is where the userId is set; I have that user subscribed to user_{theirtag}.

Does anyone have any clue how to go about sending an outbound FCM message to a specific topic? Also, how do I instantiate and include a RemoteMessage.Notification object since it cannot be done the way I have listed above?

Thanks all.

EDIT: It looks like the answer as per Mr Frank van Puffelen is that no, it cannot be done via the SDK directly. It can be done via a combination of topic subscriptions and including a server to process the actual sends (here), but it should not be done without the use of a server in order to prevent secret/ server key exposure.

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • 3
    You cannot send messages to other devices from a client-side device. You will always need to run code on a (trusted) app server for that. When you're sending an upstream message (like you are doing here), you'll need to handle it on your server: https://firebase.google.com/docs/cloud-messaging/receive-upstream. I decided it was easier to just make the messages flow through the database. See https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html – Frank van Puffelen Oct 13 '16 at 16:45
  • Ha. I was just reading that article to make this. That makes perfect sense Frank, thank you. So just to confirm, your method in the article, "'/topics/user_'+username" basically is doing what I am attempting here, but it needs to be pinged from a server to accomplish this correct? If so, is it technically possible to run an Http request from the device to replicate the same thing? (assuming I have the credentials of course). – PGMacDesign Oct 13 '16 at 16:53
  • Sending messages *to* a device requires that you pass the server key. As its name implies that key should never be present in client-side code. So it *is* technically possible (I'll find you a link) **and** it is a bad idea for any app that you will release to the public. – Frank van Puffelen Oct 13 '16 at 19:10
  • Gotcha, appreciate the advice, expertise, and time Frank. BTW, excellent job on the new FCM platform. It is beautifully written and I look forward to using it in many more projects. *digitialHighFive* – PGMacDesign Oct 13 '16 at 19:12
  • Found it: http://stackoverflow.com/a/38121573/209103 (as said it come highly unrecommended by the most authoritative sources available). Thanks for the highfive, those are *always* welcome. – Frank van Puffelen Oct 13 '16 at 19:13

0 Answers0