什么是Java等效使用https://fcm.googleapis.com/fcm/send REST Api

时间:2016-06-22 21:49:17

标签: java http post curl firebase

我已经在Curl中尝试使用以下命令来使用Firebase REST Api发送通知并且它可以正常运行:

curl -X POST --header "Authorization: key=AIza...iD9wk" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"to\": \"cAhmJfN...bNau9z\"}"

现在我试图在Java中做同样的事情,我找不到一个简单的方法来做同样的事情,我尝试的任何事情都不会触发我的移动终端中的通知。< / p>

这是我最接近的方法:

    try {
        HttpURLConnection httpcon = (HttpURLConnection) ((new URL("https://fcm.googleapis.com/fcm/send").openConnection()));
        httpcon.setDoOutput(true);
        httpcon.setRequestProperty("Content-Type", "application/json");
        httpcon.setRequestProperty("Authorization: key", "AIza...iD9wk");
        httpcon.setRequestMethod("POST");
        httpcon.connect();
        System.out.println("Connected!");

        byte[] outputBytes = "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"to\": \"cAhmJfN...bNau9z\"}".getBytes("UTF-8");
        OutputStream os = httpcon.getOutputStream();
        os.write(outputBytes);
        os.close();

        // Reading response
        InputStream input = httpcon.getInputStream();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
            for (String line; (line = reader.readLine()) != null;) {
                System.out.println(line);
            }
        }

        System.out.println("Http POST request sent!");
    } catch (IOException e) {
        e.printStackTrace();
    }

但后来我得到了:

java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at httpclient.test.MyHttpClientPost.sendNotification(MyHttpClientPost.java:131)
    at httpclient.test.MyHttpClientPost.main(MyHttpClientPost.java:26)

2 个答案:

答案 0 :(得分:5)

401表示未经授权,因此未发送有效的Authorization标头。

这一行:

httpcon.setRequestProperty("Authorization: key", "AIza...iD9wk");

不等于-H "Authorization: key=AIza...iD9wk"。第一个参数应该是标题名称,即Authorization

httpcon.setRequestProperty("Authorization", "key=AIza...iD9wk");

总之,您误解了HTTP标头的格式。基本上,标题名称和值由:而不是=分隔。

答案 1 :(得分:0)

最小样本:

public class ConnApp {
    public static void main(String[] args) throws Exception {
        HttpURLConnection httpcon = (HttpURLConnection) ((new URL("https://fcm.googleapis.com/fcm/send").openConnection()));
        httpcon.setDoOutput(true);
        httpcon.setRequestProperty("Content-Type", "application/json");
        httpcon.setRequestProperty("Authorization", "key=!!secret!!");
        httpcon.setRequestMethod("POST");
        httpcon.connect();

        byte[] outputBytes = ("{\"to\":\"!!CONFIDENTIAL!!\"," +
                "\"data\":{\"some\":\"thing\"}," +
                "\"notification\":{\"title\":\"Help\",\"body\":\"me\",\"icon\":\"me\",\"sound\":\"default\",\"badge\":\"18\"}," +
                "\"priority\":\"high\"," +
                "\"content_available\":true}").getBytes("UTF-8");
        OutputStream os = httpcon.getOutputStream();
        os.write(outputBytes);
        os.close();

        InputStream input = httpcon.getInputStream();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
            for (String line; (line = reader.readLine()) != null;) {
                System.out.println(line);
            }
        }
    }
}