42

I prepared the receiver for FCM and can send a notification to all devices.

gcm-http.googleapis.com/gcm/send with this link can send to target users who is registered and post to the target devices like below json :

 {
     "notification": {
                "title": "sample Title",
                "text": "sample text"   },   
        "to" : "[registration id]"
         }

However, I need to send notifications to target users which I choose, via email or name...etc . For example:

{
     "notification": {
                "title": "sample Title",
                "text": "sample text"   },   
        "to" : "[email or name or sex ...]"
         }

How can I do that? Do I need to create a web server or something else?

2 Answers 2

46

Did I need to create a web server

Yes. You need a place where you can map name/email to registration IDs. These registration IDs must be included in the request to FCM, for example

{
    'registration_ids': ['qrgqry34562456', '245346236ef'],
    'notification': {
        'body': '',
        'title': ''
    },
    'data': {

    }
}

will send the push to 'qrgqry34562456' and '245346236ef'.

The registration ID you use in the call is the one that's called 'token' in this callback in the app.

public class MyService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
    }
}
6
  • thanks for your response. It's helpful for me . I read almost all document from FCM.There wirte same thing like your answer. I want to be sure. because I think there is a server for notification. Jun 8, 2016 at 11:36
  • 3
    is registration_id and registration token same thing? Sep 15, 2016 at 14:22
  • 2
    @AnggaAriWijaya yes
    – Tim
    Sep 15, 2016 at 14:23
  • good , but id you can get from FirebaseInstanceId.getInstance().getToken() or class AppFirebaseMessageingService: FirebaseMessagingService() { override fun onNewToken(token: String?) { super.onNewToken(token) } override fun onMessageReceived(msg: RemoteMessage?) { super.onMessageReceived(msg)}} Nov 7, 2018 at 12:00
  • 2
    @TheWebGuy yes. How would you tell firebase who to send a message to if you can't tell them who it is?
    – Tim
    Feb 2, 2019 at 21:56
-1

you can send message to other device using this code. there is no need of server in this code.

public  String send(String to,  String body) {
            try {

                final String apiKey = "AIzaSyBsY_tfxxxxxxxxxxxxxxx";
                URL url = new URL("https://fcm.googleapis.com/fcm/send");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Authorization", "key=" + apiKey);
                conn.setDoOutput(true);
                JSONObject message = new JSONObject();
                message.put("to", to);
                message.put("priority", "high");

                JSONObject notification = new JSONObject();
               // notification.put("title", title);
                notification.put("body", body);
                message.put("data", notification);
                OutputStream os = conn.getOutputStream();
                os.write(message.toString().getBytes());
                os.flush();
                os.close();

                int responseCode = conn.getResponseCode();
                System.out.println("\nSending 'POST' request to URL : " + url);
                System.out.println("Post parameters : " + message.toString());
                System.out.println("Response Code : " + responseCode);
                System.out.println("Response Code : " + conn.getResponseMessage());

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // print result
                System.out.println(response.toString());
                return response.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }
8
  • 4
    This is not recommended way because anyone can get your API key by using tools like Smali2Java and can send the notification to anyone.
    – kunwar97
    Nov 26, 2017 at 8:27
  • that's the part of hacking, and Yeah I also don't recommend this but I was giving the answer that we can do it from here too, we can secure that part by encrypting the key if needed. and yes thanks for letting me know this. Nov 26, 2017 at 8:56
  • 1
    How will we use this? Dec 26, 2017 at 19:48
  • 10
    we can secure that part by encrypting the key - that's not true
    – Tim
    Feb 21, 2018 at 22:04
  • 6
    Do not do this. Your private api key is for your use on hardware under your control. Do not distribute it publicly. Sep 6, 2019 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.