티스토리 뷰

extension AppDelegate: UNUserNotificationCenterDelegate{
    
  // This function will be called right after user tap on the notification
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
      
    // tell the app that we have finished processing the user’s action / response
    completionHandler()
  }
}

여기서 푸시를 받아온다! 근데 iOS 13부터는 SceneDelegate가 생겨서... 굉장히 애매해졌다. 

 

window.rootViewController가 AppDelegate에서 접근 가능하게만 바꿔주면 된다. 

 

 

 func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void)
  {
    let userInfo = response.notification.request.content.userInfo

    guard var rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
      return
    }

    // change your view controller here
    rootViewController = UIViewController()

    print("====userInfo====")
    print(userInfo)
    ....
    }

이렇게 써주고 userInfo에 따라서 분기 타서 푸시 해주면 된다. 

 

아예 처음 앱을 켜서 들어온 경우는,

이 코드를 application(_:didFinishLaunchingWithOptions:) 에 넣어주면 된다. 

 // Check if launched from notification
    let notificationOption = launchOptions?[.remoteNotification]

    // 1
    if
      let notification = notificationOption as? [String: AnyObject],
      let aps = notification["aps"] as? [String: AnyObject] {
      // 2
      NewsItem.makeNewsItem(aps)
      
      // 3
      (window?.rootViewController as? UITabBarController)?.selectedIndex = 1
    }
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함