APN 푸시3 - 서버

 

1. 인증서 만들기 https://ghj1001020.tistory.com/797
2. iOS 클라이언트 https://ghj1001020.tistory.com/798
3. 서버 https://ghj1001020.tistory.com/799

 

 

pom.xml

JavaPNS 라이브러리 추가

1
2
3
4
5
6
7
        <!-- APNS -->  
        <dependency>
            <groupId>com.github.mlaccetti</groupId>
            <artifactId>javapns</artifactId>
            <version>2.3.2</version>
        </dependency>
 

 

 

ApnsServer.java

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotifications;
 
public class ApnsServer {
    
    private static final String CERT_FILE_PATH = "D:\\blog\\workspace\\web\\apns_ex.p12";    // 인증서 경로
    private static final String CERT_PASSWORD = "test1234";    // 인증서 만들때 입력한 패스워드
    private static final String DEVICE_TOKEN_ID = "1e1c9c3f4539288801101658251b3409222b20ff8b5507d7d9430985ebd96a74";    // iOS가 받은 푸시키
    
    public void sendApns() {
        PushNotificationManager pushManager = new PushNotificationManager();
        try {
            AppleNotificationServer pushServer = new AppleNotificationServerBasicImpl(CERT_FILE_PATH, CERT_PASSWORD, false);
            pushManager.initializeConnection(pushServer);
        
            List<Device> deviceList = new ArrayList<Device>();
            // device 추가
            Device device = new BasicDevice( DEVICE_TOKEN_ID );
            deviceList.add( device );
            
            PushNotificationPayload payload = PushNotificationPayload.complex();
            payload.addBadge(1);
            payload.addAlert("iOS 푸시 테스트 입니다.");
            payload.getPayload().put("message""메시지 내용을 입력해주세요.");
            
            PushedNotifications notis = pushManager.sendNotifications( payload, deviceList);
            
            int result = 0;
            if( notis != null && notis.getSuccessfulNotifications() != null ) {
                result = notis.getSuccessfulNotifications().size();
            }
        
            System.out.println"success size=" + result );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        ApnsServer server = new ApnsServer();
        server.sendApns();
    }
}
 

 

 

결과

APN 푸시2 - iOS 클라이언트

 

1. 인증서 만들기 https://ghj1001020.tistory.com/797
2. iOS 클라이언트 https://ghj1001020.tistory.com/798
3. 서버 https://ghj1001020.tistory.com/799

 

 

Signing & Capabilities > Capability 추가 버튼 클릭

- Background Modes : Remote notifications 추가

- Push Notifications 추가

 

AppDelegate.swift

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
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // 푸시 권한 획득
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isGrant, error) in
            guard isGrant else {
                return
            }
            
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
        }
        
        return true
    }
    
    
    // 푸시 토큰 받기 성공
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let deviceTokenString = deviceToken.map String(format: "%02x", $0) }.joined()
        print(deviceTokenString)
    }
    
    // 푸시 토큰 받기 실패
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print(error.localizedDescription)
    }
 
 

 

APN 푸시1 - 인증서 만들기

 

1. 인증서 만들기 https://ghj1001020.tistory.com/797
2. iOS 클라이언트 https://ghj1001020.tistory.com/798
3. 서버 https://ghj1001020.tistory.com/799

 

 

푸시 인증서 발급하기

1. 애플 개발자 사이트 로그인 > Certificates, IDs & Profiles 클릭

 

2. Certificates, Identifiers & Profiles > Certificates 추가 버튼 클릭

 

3. Create Certificate

개발용 : Services > Apple Push Notification service SSL (Sandbox) 선택

배포용 : Services > Apple Push Notification service SSL (Sandbox & Production) 선택

Platform 과 푸시 기능을 추가할려는 앱의 App ID 선택

Certificate Sigining Request 파일 선택

인증서 파일 다운로드 후 인증서를 더블 클릭하여 실행

 

 

서버 인증서 만들기

키체인 접근 실행

위에서 설치한 인증서 파일과 키를 선택하여 마우스 오른쪽 버튼 > 2개 항목 내보내기 선택

파일명, 저장 위치 선택

암호 입력 (서버에서 푸시 보낼때 입력한 암호가 필요)

 

+ Recent posts