未使用fcm接收到特定设备ID(令牌)的通知

时间:2019-04-22 20:01:43

标签: android

我正在尝试使用fcm将通知发送到特定的设备ID。我已经使用FirebaseMessagingService将令牌ID作为设备ID。问题是获得http响应200 OK后,该设备上没有收到通知。

下面是FCMService类代码:

public class FCMService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("Service ","token "+s);
}

}

这是我发送通知的方式:

       send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            pushNotification();

        }
    });

    private void pushNotification() {

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {

            String json="{ \"notification\": {\n" +
                    "    \"title\": \"Portugal vs. Denmark\",\n" +
                    "    \"text\": \"5 to 1\"\n" +
                    "  },\n" +
                    "  \"to\" : \"cGM6B4XwCUc:APA91bG-qflim6qLzciOAZvIspYm18vPKR74zA__udfH3_pqd8qYxYgLbsCpnlDIemHecGJc5coJDD9WE96_WefubuokluUs_aJSFq-M23jwofyyL7jW1iB577vNJ60S6VRjoI-02zrv\"\n" +
                    "}";

            try {

                final String apiKey = "AAAAUUkrlNs:APA91bGuCLpg6DBh2xyF-k_i91ay2hAZsTwHqtYrJUQjHBrEsOFgme746hUNarlztU2qgErRKCuAaAaD5PdPRRGbgkv6yGfGujNSnX27cKZfmDZDv4Ikl2o5mpi8R1xUf8PclCt-K6Tu";
                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);

                byte[] bytes=json.getBytes();

                Log.e("Main Activity","json "+json);

                conn.setFixedLengthStreamingMode(bytes.length);

                OutputStream stream= conn.getOutputStream();
                stream.write(bytes);

                Log.e("Main Activity","code "+conn.getResponseCode());


                int responseCode = conn.getResponseCode();
                System.out.println("\nSending 'POST' request to URL : " + url);
                System.out.println("Post parameters : " + json.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());

            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    });


}

我该怎么做才能收到该设备的通知?请帮助

0 个答案:

没有答案