IOS 13 부터 SceneDelegate가 추가되면서 AppDelegate와 두군데에서 처리해 줘야 된다.

SceneDelegate를 프로젝트에서 삭제시 AppDelegate에서만 처리 가능

 

test.apns 파일 생성

/* 시뮬레이터 테스트용 test.apns 파일
  test.apns 파일을 생성해서 시뮬레이터에 드레그 하면 알림이 발생
*/
{
  "aps": {
    "alert": {
      "title": "Push 테스트",
      "subtitle" : "Notification subtitle",
      "body": "🥳 Woohoo! Push notification in simulator! 🎉",
      "sound": "default"
    },
    "badge": 1
    
  },
  "extra" : {
     "vod_key" : "1234",
     "img_url" : "http://image.jpg",
     "api_url" : "http://bulabula.com/test"
  },
  "Simulator Target Bundle": "내 앱 번들아이디 넣는곳"
}

 

1. IOS 12 이하 일경우 

class AppDelegate: UIResponder, UIApplicationDelegate {

    var pushInfo: NSDictionary?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
                             ...... 생략 ......
        
    	// 앱이 종료된 상태에서 APN Push 클릭시 extra 데이터 처리 (IOS 12이하)
        if let notification = launchOptions?[.remoteNotification] as? [String:AnyObject]{
            
            let aps = notification["aps"] as? NSDictionary
            let extra = notification["extra"] as? NSDictionary
            pushInfo = extra
            
            // Json String으로 변환 후 Model Data 변환처리
//            let extraString = notification["extra"] as! String
//
//            log(direction: .RECEIVE, ofType: self, datas: "extra 3 : \(extraString)")
//
//            let replaceString = extraString.replacingOccurrences(of: "\\", with: "")
//            if let extra = APNSMessageModel.from(json: replaceString) {
//                pushData = extra
//            }
        }
                             ...... 생략 ......
    }

 

2. SceneDelegate 가 있는 경우

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var pushInfo: NSDictionary?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    	guard let _ = (scene as? UIWindowScene) else { return }
        
        // 앱이 종료된 상태에서 APN Push 클릭시 extra 데이터 처리
        if let notificationResponse = connectionOptions.notificationResponse {
            log(direction: .PUSH, ofType: self, datas: "connectionOptions : \(notificationResponse.notification.request.content.userInfo)")
            
            let aps = notificationResponse.notification.request.content.userInfo["aps"] as! NSDictionary
            let extra = notificationResponse.notification.request.content.userInfo["extra"] as! NSDictionary
            pushInfo = extra
        }
    
    }

3. 들어온 데이터 확인 테스트

class ViewController: UIViewController {
	override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func pushDataInfoViewTest() {
        // 앱종료된 상태서 Extra Data 사용시 Test Code
        if #available(iOS 13.0, *) {
            let sceneDelegate = UIApplication.shared.connectedScenes
                .first!.delegate as! SceneDelegate
            
            if let pushInfo = sceneDelegate.pushInfo {
                
                log(direction: .PUSH, ofType: self, datas: "SceneDelegate Push Info: \(pushInfo)")
                
                let alertController = UIAlertController(title: "SceneDelegate Push Info", message: pushInfo.description, preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                    
                }))
                
                guard let topVC = UIApplication.currentTopViewController() else {
                    return
                }
                
                topVC.present(alertController, animated: true, completion: nil)
            }
        } else { // iOS12 or earlier
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            
            if let pushInfo = appDelegate.pushInfo {
                
                log(direction: .PUSH, ofType: self, datas: "AppDelegate Push Info: \(pushInfo)")
                
                let alertController = UIAlertController(title: "AppDelegate Push Info", message: pushInfo.description, preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                    
                }))
                
                guard let topVC = UIApplication.currentTopViewController() else {
                    return
                }
                
                topVC.present(alertController, animated: true, completion: nil)
            }
        }
}

 

+ Recent posts