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)
    }
 
 

 

+ Recent posts