Java发送FCM通知missmatchSenderId

时间:2020-04-26 21:04:53

标签: java firebase-cloud-messaging

我正在尝试使用Firebase云消息发送推送消息,并收到以下响应:

{“ multicast_id”:1297701738321054450,“成功”:0,“失败”:1,“ canonical_ids”:0,“结果”:[{“错误”:“ MismatchSenderId”}]}}

我曾经使用旧版服务器密钥,但在Firebase控制台中找不到

public final static String AUTH_KEY_FCM = "AAAAOQVZkBU:APA91bG2yzutjcqiYw0HhB8D1--fCOyqgFl9w1DfwkAoo8yFRsGj65lMEu22-OsqQEGu8y01bYoxSgfwgkUItHjEjkJEPJAzW3r4GalttiXXXXXXXXXXXXXXXXXXXXXXXXX";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public final static String DEVICE_ID = "DEVICE_TOKEN_HERE";
public static void main(String[] args) {
    String DeviceIdKey = "TOKEN";
    String authKey = AUTH_KEY_FCM;
    String FMCurl = API_URL_FCM;
    StringBuffer response = new StringBuffer();
    HttpURLConnection conn = null;
    try {
        URL url = new URL(FMCurl);
        conn = (HttpURLConnection) url.openConnection();

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

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "key=" + authKey);
        conn.setRequestProperty("Content-Type", "application/json");
        JSONObject data = new JSONObject();
        data.put("to", DeviceIdKey);
        JSONObject info = new JSONObject();
        info.put("title", "test"); // Notification title
        info.put("body", "test"); // Notification body
        info.put("sound", "default"); // Notification body
        info.put("extra_title", "ex1"); // Notification body
        info.put("extra_body", "ex2"); // Notification body
        data.put("data", info);// to be able to control from client side show/hide
        System.out.println(data.toString());
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data.toString());
        wr.flush();
        wr.close();

        int responseCode = conn.getResponseCode();
        System.out.println("Response Code : " + responseCode);

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

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        System.out.println("Resonse: " + response);

    } catch (Exception e) {
        System.out.println(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }

    }



}

0 个答案:

没有答案