从Java或JS发布到GCM

时间:2015-10-29 18:20:14

标签: http push-notification google-cloud-messaging

我知道有很多不同的情况类似于我的stackoverflow,但我无法建立连接。

我要做的是向GCM发送一个简单的推送通知。我找到了两个我尝试POST的链接。请注意,这些链接在我找到的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表示不接受方法(链接以供参考http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)。

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

方法1:

        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);

方法2

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。

方法3:

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);
      }

1 个答案:

答案 0 :(得分:1)

这已经使用Java方法解决了。 JS继续返回400,401,411等状态代码。事实证明,Java返回200但我的手机没有收到任何东西的原因是因为我的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成员提供了此解决方案。