POST 从 Java 或 JS 到 GCM

POST from Java or JS to GCM

我知道在 Whosebug 中有许多与我相似的不同情况,但我就是无法建立联系。

我想做的是向 GCM 发送一个简单的推送通知。我发现了两个 link,我也尝试 POST。请注意,这两个 link 都在我发现的这个 PHP 脚本中工作。

https://gcm-http.googleapis.com/gcm/send

https://android.googleapis.com/gcm/send

我尝试从 JS 向 GCM 发送推送通知,但是许多人表示由于安全问题不能这样做。截至目前,当我在 Angular JS 中执行我的代码时,我从 https://gcm-http.googleapis.com/gcm/send 收到 405 错误。状态 405 表示方法未被接受(link 供参考 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).

这是JS的代码。我尝试了两种方法。

方法一:

        var xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //ite
            }
        };
        var jsonCall = {
            registration_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx-AEQtUUWnCVH566xcwib4HinI16W3_g"
        };
        xmlhttp.open("POST", "https://gcm-http.googleapis.com/gcm/send", true);
        xmlhttp.setRequestHeader("Content-type", "application/json"); 
        xmlhttp.setRequestHeader("Authorization", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        xmlhttp.send(jsonCall);

方法二

var jsonCall = {
            registration_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx-AEQtUUWnCVH566xcwib4HinI16W3_g"
        };
        $http({
            method:'POST',
            url: 'https://gcm-http.googleapis.com/gcm/send',
            data: jsonCall,
            headers: {
                'Authorization': 'A1nxxxxxxxxxxxxxxxxxx',
                'Content-type': 'application/json' }
        })

这是我在Java中尝试过的。请注意,我的项目不是作为 Android 项目创建的,而是作为普通的 Java 项目创建的。我在这里收到 411 错误,所以我认为我用作 JSON 的字符串不正确。请注意,如果我使用 GET,我会得到 200。

方法三:

HttpURLConnection connection = null;  
      try {
        //Create connection
        String json ="{\"registration_ids\":[\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxx\"]}";
        URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
        connection = (HttpURLConnection)url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setRequestProperty("Content-Length", "0");
        connection.setRequestProperty("Authorization", "key="+"xxxxxxxxxxxxxxxxxxxxxxxxxx");



        System.out.println(connection.getResponseCode());
        InputStream stream = (InputStream) connection.getInputStream();
        InputStreamReader isReader = new InputStreamReader(stream); 

        //put output stream into a string
        BufferedReader br = new BufferedReader(isReader);
        String line;
        while((line = br.readLine()) != null){
            System.out.println(line);
        }

        OutputStream os = connection.getOutputStream();
        os.write(json.getBytes("UTF-8"));
        os.flush();
        os.close();



      } catch(Exception e){
          System.out.println(e);
      }

如果有人能看一看,并为我指明正确的方向,我将不胜感激。

更新:

我已经摆脱了 411 错误。我认为这是因为我从来没有联系过。现在我得到了正确的 200 代码,但推送通知没有发送。我的 JSON 格式正确吗?

 HttpURLConnection connection = null;  
      try {
        //Create connection
        String json ="{\"registration_ids\":[\"APA91bGxHWapgmxgyvPceu85ArDMLaFekbTt5RGzy3gv1xtSO09tJbvnaeVLefBqNl_iBrctoZQ2AltSMfrXykq8-AEQtUUWnCVH566xcwib4HinI16W3_g\"]}";
        URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
        connection = (HttpURLConnection)url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");      
        connection.setRequestProperty("Authorization", "key=xxxxxxxxxxxxxxxxxxxxxxx");


        connection.connect();
        OutputStream os = connection.getOutputStream();
        os.write(json.getBytes("UTF-8"));
        System.out.println(connection.getResponseCode());
        InputStream stream = (InputStream) connection.getInputStream();
        InputStreamReader isReader = new InputStreamReader(stream); 

        //put output stream into a string
        BufferedReader br = new BufferedReader(isReader);
        String line;
        while((line = br.readLine()) != null){
            System.out.println(line);
        }


        os.flush();
        os.close();



      } catch(Exception e){
          System.out.println(e);
      }

已使用 Java 方法解决此问题。 JS 不断返回 400、401、411 等状态代码。原来 Java 返回 200 而我的 phone 没有收到任何东西的原因是我的 JSON 不正确。这是正确的 JSON 值:

String postData = "{ \"registration_ids\": [ \"" + CLIENT_REG_ID + "\" ], " +
                "\"delay_while_idle\": true, " +
                "\"data\": {\"tickerText\":\"My Ticket\", " +
                "\"contentTitle\":\"My Title\", " +
                "\"message\": \"Test GCM message from GCMServer-Android\"}}";

这是从我发布的另一个问题中获得的,其中一位 SO 成员提供了这个解决方案。