How to send notification to specific users with FCM?

前端 未结 2 785
刺人心
刺人心 2020-12-02 20:28

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 user

相关标签:
2条回答
  • 2020-12-02 21:10

    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";
            }
    
    0 讨论(0)
  • 2020-12-02 21:14

    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() {
        }
    }
    
    0 讨论(0)
提交回复
热议问题