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 값을 넣어주어야 한다.


댓글 없음:

댓글 쓰기