使用以下代碼,我可以向Firefox發送通知。它工作正常。如何使用Java將推送通知發送到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();
}
}
但是,當我試圖發送標題,正文和網址uanble發送到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();
}
}
任何人都可以幫助我解決這個問題嗎?
在你的第二個代碼片段,你執行你的設置頁眉和實體屬性之前,您的請求。 – f1sh
很抱歉,它錯別字..我在設置標題後執行代碼。我已更新代碼 – Thulasiram