从Java或JS到GCM的POST [英] POST from Java or JS to GCM

查看:247
本文介绍了从Java或JS到GCM的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know there are many different situations that resemble mine across stackoverflow, but I just couldn't make the connection.

我要做的是向GCM发送一个简单的推送通知。我找到了两个我尝试POST的链接。请注意这些链接在我找到的PHP脚本中有效。

What I am trying to do, is to send a simple push notification to the GCM. I found two links for which I try to POST too. Note both these links work in this PHP script i found.

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

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

我试图发送推送从JS到GCM的通知,但很多人都说这不能因为安全问题。截至目前,当我在Angular JS中执行我的代码时,我收到来自 https:/的405错误/gcm-http.googleapis.com/gcm/send 。状态405表示不接受的方法(参考链接 http://www.w3.org /Protocols/rfc2616/rfc2616-sec10.html )。

I tried to send push notifications from JS to the GCM, but many people have stated that this can not because of security issues. As of now, when I execute my code in Angular JS, I am getting a 405 error from https://gcm-http.googleapis.com/gcm/send. Status 405 means method not accepted (link for reference http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).

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

Here is the code for JS. I have two method that I tried.

方法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.

This is what I have tried in Java. Note that my project was not created as an Android project, but just as a normal Java project. I get a 411 error here, so I think the string I use as JSON is incorrect. Note that I get a 200 if I use GET.

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

如果有人可以看看这个,并指引我正确的方向,我真的很感激。

If someone can take a look at this, and set me in the correct direction, I would really appreciate it.

更新:

我已经摆脱那个411错误。我认为这是因为我从来没有连接过。现在我得到正确的200代码,但推送通知不会发送。我的JSON格式是否正确?

I have gotten rid of that 411 error. I think it was because I never connected in the first place. Now I am getting the correct 200 code, but the push notification does not send. Is my JSON the correct format?

 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但我的手机没有收到任何东西的原因是因为我的JSON不正确。以下是正确的JSON值:

This has been solved using the Java method. JS keeps on returning those status codes of 400, 401, 411 etc. It turns out the reason Java returned a 200 but my phone did not receive anything was because my JSON was incorrect. Here is the correct JSON value:

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成员提供了此解决方案。

This was obtained from another question I posted, where a fellow SO member provided this solution.

这篇关于从Java或JS到GCM的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆