如何使用FCM向特定用户发送通知?

时间:2016-06-08 11:18:04

标签: android notifications firebase google-cloud-messaging firebase-cloud-messaging

我为FCM准备了接收器,可以向所有设备发送通知。

gcm-http.googleapis.com/gcm/send 此链接可以发送给已注册并发布到目标设备的目标用户,如下面的json:

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

但是,我需要向我选择的目标用户发送通知, 通过电子邮件或姓名等。 例如:

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

我该怎么做? 我是否需要创建Web服务器或其他东西?

2 个答案:

答案 0 :(得分:27)

  

我是否需要创建Web服务器

是。您需要一个可以将姓名/电子邮件映射到注册ID的地方。这些注册ID必须包含在FCM的请求中,例如

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

    }
}

会将推送发送给' qrgqry34562456'和' 245346236ef'。

您在通话中使用的注册ID是一个名为'令牌'在应用程序的回调中。

public class MyService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
    }
}

答案 1 :(得分:1)

您可以使用此代码向其他设备发送消息。此代码中不需要服务器。

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

                final String apiKey = "AIzaSyBsY_tfCQjUSWEWuNNwQdC1Vtb0szzz";
                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";
        }
相关问题