Java Android FCM向用户服务器发送消息返回HTTP响应码

标签 java android firebase firebase-cloud-messaging

我这样做了:

public class PushNotifictionHelper {
    public final static String AUTH_KEY_FCM = "AIzaSyD63pfTvnwhe9WVuIe.........";
    public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

    public static String sendPushNotification(String deviceToken)
            throws IOException, JSONException {
        String result = "";
        URL url = new URL(API_URL_FCM);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
        conn.setRequestProperty("Content-Type", "application/json");

        JSONObject json = new JSONObject();

        json.put("to", deviceToken.trim());
        JSONObject info = new JSONObject();
        info.put("title", "notification title"); // Notification title
        info.put("body", "message body"); // Notification
        // body
        json.put("notification", info);
        try {
            OutputStreamWriter wr = new OutputStreamWriter(
                    conn.getOutputStream());
            wr.write(json.toString());
            wr.flush();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            result = "OK";
        } catch (Exception e) {
            e.printStackTrace();
            result = "BAD";
        }
        System.out.println("GCM Notification is sent successfully");

        return result;

    }

    public static void main(String [] args){
        try {
            PushNotifictionHelper.sendPushNotification("ep51x3Ckmig:APA91bG4PdoJC7zGlV0JPmCA49jmqJCkeSPH1QzF9byxdH1nRlFOVyAi9ppO2ygoSpp8s44o1oGO8n-HCJDB_oZAZ6WCwFD2a9yAFmKIpKhmPXakeLf-ktqPnzwf-GFziv7_nMdVPIci");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

当我在控制台中运行它时,我看到了

java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send

我从网络上获得的 AUTH_KEY_FCM:

Klucz interfejsu Web API AIzaSyD63pfTvnwhe9WVuIe.........

最佳答案

401 错误是指身份验证错误。来自docs :

The sender account used to send a message couldn't be authenticated. Possible causes are:

  • Authorization header missing or with invalid syntax in HTTP request.
  • Invalid project number sent as key.
  • Key valid but with FCM service disabled.
  • Request originated from a server not whitelisted in the Server key IPs.

Check that the token you're sending inside the Authentication header is the correct Server key associated with your project. See Checking the validity of a Server key for details.

使用 FCM 时,您应该始终使用云消息传递 选项卡中的服务器 key (而不是 Web API key )在你的Firebase Console .

关于Java Android FCM向用户服务器发送消息返回HTTP响应码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43093275/

相关文章:

java - 使用 greenDAO 实现接口(interface)时出错

javascript - 异步 https Firebase Google Cloud 功能失败,错误为 "function crashed"/"socket hang up"

android - Firebase 仪表板不显示价格值

带有通配符和静态包装器的 Java 泛型

java - 如何在Android Studio中对 ListView 中的数据进行排序

java - 分析工具如何在某些按钮单击或拍摄堆快照时执行垃圾收集?

android - 在 Android 中实现搜索过滤器

firebase - Google Cloud Platform - 将 "no organization"的 firebase 项目移动到 "organization"时出错

java - 如何在非 TabActivity 的 Activity 中创建 tabHost

java - 将字符串转换为 float 而不进行四舍五入java