Firebase云消息传递主题

时间:2017-07-11 11:23:03

标签: java firebase firebase-cloud-messaging

这是我的代码,我正在尝试设置firebase调用以向主题发送消息。我得到了200响应代码,但FCM控制台上没有任何内容。难道我做错了什么。

public static void pushFCMNotification() throws Exception{

   String authKey = Constants.AUTH_KEY_FCM; 
   String FMCurl = Constants.API_URL_FCM;
   URL url = new URL(FMCurl);
   HttpURLConnection 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 json = new JSONObject();
        JSONObject info = new JSONObject();
        try {
            info.put("title", "New notification"); // Notification title
            info.put("body", "A new notification has been added to the notice board"); // Notification body
            json.put("notification", info);
            json.put("to", "/topics/notif"); //replace userDeviceIdKey with the unique notification key for the group
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(json.toString());
        wr.flush();
        conn.getInputStream();
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

通过REST API发送的消息不会出现在控制台中(无论是发送到令牌还是主题)。

通常,如果您使用REST API发送到令牌,则可以在“诊断”页面中查看它。但是,发送到主题的消息也不会出现在那里。 (请参阅我在评论部分中链接的可能重复的帖子)