在ios问题中使用Google云消息传递(gcm)推送通知

时间:2016-01-27 06:03:37

标签: java cordova phonegap-plugins

我正在尝试通过GCM从JAVA发送IOS通知消息。那个时候我收到一个400状态代码Bad Request的错误。下面我提到了我的JAVA代码......

 String apiKey = "xxxxxxxxxxxxxxxxxxxx"; // Put here your API key
        String GCM_Token = regid; // put the GCM Token you want to send to here
String notification = "{\"sound\":\"default\",\"badge\":\"2\",\"title\":\"default\",\"body\":\"Test Push!\"}"; // put the message you want to send here
            String messageToSend = "{\"to\":\"" + GCM_Token + "\",\"notification\":" + notification + ",\"content_available\" : true}"; // Construct the message.

TRY { 网址url =新网址(" https://android.googleapis.com/gcm/send");

            System.out.println("Message"+messageToSend);
            // Open connection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();



            //Set the headers
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setRequestProperty("Authorization", "key=" + apiKey);
            conn.setDoOutput(true);


           DataOutputStream wr = new DataOutputStream(conn.getOutputStream());


          wr.write(messageToSend.getBytes("UTF-8"));

            //Send the request and close
            wr.flush();
            wr.close();

            //Get the response
            int responseCode = conn.getResponseCode();

            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);
            System.out.println("Message : " + 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()); //this is a good place to check for errors using the codes in http://androidcommunitydocs.com/reference/com/google/android/gcm/server/Constants.html

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

请给我一个建议。谢谢。

0 个答案:

没有答案
相关问题