0

I am trying to figure out user to user notification call using FCM in android, I have an example of "subscribeToTopic("TOPIC Name")", but instead of this I am not able to figure out how to apply user to user notification using token id of the users in FCM. I am referring this tutorial https://blog.usejournal.com/send-device-to-device-push-notifications-without-server-side-code-238611c143

please assist, thanks in advance

btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TOPIC = "/topics/userABC"; //topic must match with what the receiver subscribed to
                NOTIFICATION_TITLE = edtTitle.getText().toString();
                NOTIFICATION_MESSAGE = edtMessage.getText().toString();

                JSONObject notification = new JSONObject();
                JSONObject notifcationBody = new JSONObject();
                try {
                    notifcationBody.put("title", NOTIFICATION_TITLE);
                    notifcationBody.put("message", NOTIFICATION_MESSAGE);

                    notification.put("to", TOPIC);
                    notification.put("data", notifcationBody);
                } catch (JSONException e) {
                    Log.e(TAG, "onCreate: " + e.getMessage() );
                }
                sendNotification(notification);
            }
        });

btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TOPIC = "/topics/userABC"; //topic must match with what the receiver subscribed to
                NOTIFICATION_TITLE = edtTitle.getText().toString();
                NOTIFICATION_MESSAGE = edtMessage.getText().toString();

                JSONObject notification = new JSONObject();
                JSONObject notifcationBody = new JSONObject();
                try {
                    notifcationBody.put("title", NOTIFICATION_TITLE);
                    notifcationBody.put("message", NOTIFICATION_MESSAGE);

                    notification.put("to", TOPIC);
                    notification.put("data", notifcationBody);
                } catch (JSONException e) {
                    Log.e(TAG, "onCreate: " + e.getMessage() );
                }
                sendNotification(notification);
            }
        });
  • make an AsyncTask that post JSON data that target your topic in this API https://fcm.googleapis.com/fcm/send and make NotificationService that extends with FirebaseMessagingService – L2_Paver Oct 11 '19 at 08:32
  • refer to this https://stackoverflow.com/questions/38237559/how-do-you-send-a-firebase-notification-to-all-devices-via-curl – L2_Paver Oct 11 '19 at 08:42
  • @L2_Paver the link I shared is informing me about the topic based notificaiton, I need to know about the notification just a user commented on your post, that kind of user to user notification using token id, how we should implement that? –  Oct 11 '19 at 08:56
  • the same procedure just like my last comment just change the JSON data that target a specific token id. The JSON data that you Post on that API control your broadcast function either its single device, group of devices or base on topics – L2_Paver Oct 11 '19 at 09:02
  • @L2_Paver i am a newbie, it will be very helpful if you show some code –  Oct 11 '19 at 09:08

1 Answers1

0

Use this kind of JSON format to send notification to a specific device.

{ "notification": {
    "title": "Title Message",
    "text": "Message here"
  },
  "to" : "YOUR_TARGET_device_token_id"
}
L2_Paver
  • 596
  • 5
  • 11