如何使用Java向Firefox浏览器发送推送通知

时间:2016-05-12 12:55:57

标签: java firefox push-notification

使用以下代码我可以向Firefox发送通知。它运作正常。

 public static void main(String args[]){
            HttpClient httpClient = new DefaultHttpClient();
            try {
                String furl = "https://updates.push.services.mozilla.com/push/v1/gAAAAABXM0VF8ptA2Tl38W4b9g9Dpm0Ek2G1ZTztq0USYSYKiOicOkcB0XEewj_Oh0UnwRbia8mMxmfoWL1kyw4ix5PKFS943hYbOo-PGqpPe7qkp4GYC4wk2Mk8Yy1Yyuj6FhM_S-ec";

                HttpPut request = new HttpPut(furl); 
                HttpResponse response = httpClient.execute(request); 

                Integer responseCode = response.getStatusLine().getStatusCode();
                System.out.println(responseCode);
            }catch(Exception e){
                e.printStackTrace();
            }
        }

但是,当我试图发送标题,正文和URL无法发送到Firefox。我收到响应代码为400

     public static void main(String args[]){
            HttpClient httpClient = new DefaultHttpClient();
            try {
    String furl = "https://updates.push.services.mozilla.com/push/v1/gAAAAABXM0VF8ptA2Tl38W4b9g9Dpm0Ek2G1ZTztq0USYSYKiOicOkcB0XEewj_Oh0UnwRbia8mMxmfoWL1kyw4ix5PKFS943hYbOo-PGqpPe7qkp4GYC4wk2Mk8Yy1Yyuj6FhM_S-ec";
                JSONObject msg = new JSONObject();
                msg.put("title", "Title");
                msg.put("body", "Body");
                JSONObject json = new JSONObject();
                json.put("data", msg); 
                HttpPut request = new HttpPut(furl); 

                request.setHeader("Content-type", MediaType.APPLICATION_JSON); 
                StringEntity params = new StringEntity(json.toString());        
                request.setEntity(params);
HttpResponse response = httpClient.execute(request); 
                Integer responseCode = response.getStatusLine().getStatusCode();
                System.out.println(responseCode);
            }catch(Exception e){
                e.printStackTrace();
            }
        }

任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我认为您的代码中存在拼写错误:Content- T ype

request.setHeader("Content-Type", MediaType.APPLICATION_JSON);