0

Not able to send push notifications to specific users using device tokens.

How can I send push notifications using only device-tokens to specific user obtained from FirebaseMessaging in android. I actually want to notify users whether their tickets are verified or rejected , here is my code please and help me out please,

private void sendNotificationRequest(String title, String message,String deviceToken) throws JSONException {
     
    JSONObject mainObject = new JSONObject();
    mainObject.put("to","/"+deviceToken);
    JSONObject notificationDataObject = new JSONObject();
    notificationDataObject.put("title",title);
    notificationDataObject.put("body",message);
    mainObject.put("notification",notificationDataObject);

    JsonObjectRequest jsonObjectRequest =  new JsonObjectRequest(Request.Method.POST, POST_BASE_URL,
            mainObject,
            response -> {

            }, error -> {

    }
    )
    {
        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> header = new HashMap<>();
            header.put("content-type","application/json");
            header.put("authorization","key="+SERVER_KEY);
            return header;
        }
    };

    requestQueue.add(jsonObjectRequest);
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If I understand this correctly, you are trying to send a message from a server environment. Have you considered using FirebaseMessaging.getInstance().send(message); per https://firebase.google.com/docs/cloud-messaging/send-message#java ? – 10101010 Feb 08 '21 at 04:04
  • I have explained in one of my **[tutorials](https://www.youtube.com/playlist?list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee)** step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Cloud Firestore` and `Node.js`. You can also take a look at my answer from this **[post](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)**. – Alex Mamo Feb 08 '21 at 09:33
  • I have user device tokens stored in firestore, I want to send notification to target user using his device tokens ! – Nawed Akhtar Feb 08 '21 at 18:32

0 Answers0