티스토리 뷰
GCM iOS Push 보내기!
String apiKey = ""; // 발급받은 kEY
String GCM_Token = regId; // Registration ID
// Json 데이터 세팅
String notification = "{\"sound\":\"default\",\"badge\":\"2\",\"title\":\"\",\"body\":\""+ contents +"\"}";
String contentsData = "{\"원하는데이터\":\""+원하는데이터"}";
String messageToSend = "{\"to\":\"" + GCM_Token + "\",\"notification\":" + notification + ",\"data\":" + contentsData + "}";
try {
// URL
URL url = new URL("https://android.googleapis.com/gcm/send");
System.out.println(messageToSend);
// Open connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Specify POST method
conn.setRequestMethod("POST");
//Set the headers
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey);
conn.setDoOutput(true);
//Get connection output stream
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
byte[] data = messageToSend.getBytes("UTF-8");
wr.write(data);
//Send the request and close
wr.flush();
wr.close();
//Get the response
int responseCode = conn.getResponseCode();
//System.out.println("\nSending 'POST' request to URL : " + url);
// System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParser.parse(response.toString());
JSONObject sendResult = (JSONObject) jsonObj;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
위 코드는 상용에서 사용하고 있는 코드이며 기존 Android Push기능을 GCM을 통해 수행하고 있었으며, iOS Push 기능을 추가 함
끗!!!!!!
'Java & Spring & SpringBoot' 카테고리의 다른 글
java.net.SocketTimeoutException: Read timed out 에러 해결 방법 (0) | 2018.02.22 |
---|---|
WebLogic 설치방법&어플리케이션 추가 (0) | 2018.01.19 |
localhost, ip 도메인 연결 방법 (0) | 2017.12.18 |
log4j level 설정 방법 및 level 별 설명 (0) | 2017.11.24 |
web.xml & context-application.xml 사용 내용 정리 (0) | 2017.10.12 |
- Total
- Today
- Yesterday
- 군산 게스트하우스
- 군산 가볼만한곳
- 고흥 여행
- 제주도 스쿠터 여행
- 베스파LX
- 남자 혼자 여행
- 보성 여행
- 전국 일주 여행
- 국제반점
- Docker
- 은파호수공원
- 군산 여행
- 송내 룸카페
- 담양 여행
- kendo grid
- 스쿠터 여행
- 돌장갑
- 인터바이크
- vscode
- 초원사진관
- 송내역 카페
- 영광 여행
- 오라클 DB 링크
- 나래바이크
- 송내 카페
- 리니지m
- 송내역 룸카페
- 윈드스크린
- 스쿠터 전국 일주
- 남자 혼자 국내 여행
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |