2016년 1월 25일 월요일

[iOS] Apple Push Notification Service(APNS) Java Server Example

Apple Push Notification Service(APNS) Server 간단 예제.

해당 예제는 javapns를 통해서 간략히 만든 것임을 밝힘.
javapns의 풀 소스를 보시려면 아래의 링크를 따라 가면 됨.
Google Code Archive - JavaPNS

[ 준비 사항 ]
- JavaPNS_2.2.jar (위의 링크에서 다운 가능)


1:  import javapns.Push;  
2:  import javapns.notification.PushNotificationPayload;  
3:    
4:  public class NotificationTest {  
5:    
6:       private static String token = "Device Token";  
7:       private static String keystore = "The path of the .p12 file";  
8:       private static String password = "The password of the .p12 file";  
9:    
10:       public static void main(String[] args) throws Exception {  
11:    
12:            System.out.println("Setting up Push notification");  
13:    
14:            try {  
15:                 // Setup up a simple message  
16:                 PushNotificationPayload payload = new PushNotificationPayload();  
17:                 payload.addAlert("PushTest");  
18:                 payload.addBadge(1);  
19:                 payload.addSound("default");  
20:                 System.out.println(payload);  
21:                 System.out.println("Payload setup successfull.");  
22:    
23:                 // Push.payload(Payload payload, Object keystore, String password, boolean production, Object devices)  
24:                 // production (true/false)  
25:                 // true : Distribute (gateway.push.apple.com)  
26:                 // false : Developer (gateway.sandbox.push.apple.com)  
27:                 Push.payload(payload, keystore, password, false, token);  
28:    
29:            } catch (Exception e) {  
30:                 e.printStackTrace();  
31:            }  
32:       }  
33:  }  
  • private static String token : 토큰의 경우 App에서 APNS 등록 할 때 사용하는 token
  • private static String keystore : .p12 파일의 경로 
  • private static String password : .p12 파일 생성시 설정한 비밀번호
  • APNS 서버 주소는 개발용과 배포용이 다르기 때문에 개발 중인 App의 인증서에 따라 Push.payload의 production의 값 변경 (true:배포용, false:개발용)
iOS 8이전의  경우 서버에서 보내주는 푸시 메시지 형식에 sound에 관련하여 넣지 않으면 default가 적용되었지만 iOS 8부터는 필수로 sound 값을 넣어주어야 한다.


[iOS] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.

iOS 9 및 OSX 10.11 ElCapitan 업데이트에 App Transport Security(ATS) 가 추가 되었다.

iOS 9 인 iPhone(이하 기기)에서 기존 서버의 웹 페이지에 접속이 안되는 것을 발견하고 급당황
더 당황스러운 것은 이제서야 발견 했다는거 .... (테스트 폰을 iOS 9으로 업데이트를 안했다는 것이 함정)

암튼 기기에서 http:// 로 시작 하는 웹 페이지로 접속 하려고 하면 아래와 같은 로그를 볼 수 있다.

" App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file "

App Transport Security 가 HTTP (암호화 되지 않은 텍스트) 로의 접속을 보안상 안전하지 않은 이유로 막았고 임시 예외처리를 Info.plist를 통해 설정할 수 있다.

Info.plist로 예외처리를 할 수 있다지만 일시적이라는 것이 맘에 걸리긴 하지만 당장 접속이 되어야 하기 때문에 설정을 해 본다

우선 App Transport Security (ATS)가 무엇인고 하니.

iOS Developer Libray에 친절히 적어 놓으셨다. What's New in iOS
결론적으로 말하자면 보안상의 이유로 HTTPS를 기본 접속으로 설정 해 놓았다 머 이런 얘기.

위에서 말한대로 Info.plist에 일시적인 예외처리 설정을 통해서 App Transport Security(ATS)를 비활성화 해줄 수 있다.

NSAppTransportSecurity -> NSAllowsArbitraryLoads : true.


Info.plist에서 해당 항목이 보이지 않으면 Info.plist를 Source code로 열면 xml 형식으로 보여준다.


위의 항목을 추가 시켜 주면 끝.

하나 해결 했굼.