2017-09-12 14 views
0

내 애플 리케이션에 firebase 알림을 빌드합니다. 성공적인 작업입니다. 하지만 알림 데이터가 "Second"와 동일한 경우 열려고하고 내 앱에서 SecondViewController를 엽니 다. 어떻게해야합니까? 나는 userInfo를 사용하지만 그것을하지 않았다.Firebase Notification Swift 3 SecondViewController

Firebase Notification Special Data: { "view" : "Second" } 

이것은 내 앱 코드입니다. 고맙습니다. 나는 내 영어로 유감이다. 이 코드를 추가 userNotificationCenter 내부
사용자 정보에서

import UIKit 
import Firebase 
import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate } 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     FirebaseApp.configure() 
     application.registerForRemoteNotifications() 
     requestNotificationAuthorization(application: application) 
     if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary { 

      NSLog("[RemoteNotification] applicationState: \(applicationStateString) didFinishLaunchingWithOptions for iOS9: \(userInfo)") 
      //TODO: Handle background notification 
     } 
     return true 
    } 

    var applicationStateString: String { 
     if UIApplication.shared.applicationState == .active { 
      return "active" 
     } else if UIApplication.shared.applicationState == .background { 
      return "background" 
     }else { 
      return "inactive" 
     } 
    } 

    func requestNotificationAuthorization(application: UIApplication) { 
     if #available(iOS 10.0, *) { 
      UNUserNotificationCenter.current().delegate = self 
      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in }) 
     } else { 
      let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
     } 
    } 
} 

@available(iOS 10, *) 
extension AppDelegate : UNUserNotificationCenterDelegate { 
    // iOS10+, called when presenting notification in foreground 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     let userInfo = notification.request.content.userInfo 
     NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) willPresentNotification: \(userInfo)") 
     //TODO: Handle foreground notification 
     completionHandler([.alert]) 
    } 

    // iOS10+, called when received response (default open, dismiss or custom action) for a notification 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     let userInfo = response.notification.request.content.userInfo 
     NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) didReceiveResponse: \(userInfo)") 
     //TODO: Handle background notification 
     completionHandler() 
    } 
} 

extension AppDelegate : MessagingDelegate { 
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { 
     NSLog("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)") 
    } 

    // iOS9, called when presenting notification in foreground 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 

     NSLog("[RemoteNotification] applicationState: \(applicationStateString) didReceiveRemoteNotification for iOS9: \(userInfo)") 
     if UIApplication.shared.applicationState == .active { 
      //TODO: Handle foreground notification 
     } else { 
      //TODO: Handle background notification 
     } 
    } 
} 
+0

그냥 표시 할 경우 ... 그렇지 않으면 ... 그것은 rootViewController하게 탐색 컨트롤러에 밀어 넣습니다. .. 당신은 직접 창 개체를 사용할 수 있습니다. 처음보기 컨트롤러에 데이터가 포함 된 로컬 NSNotification을 게시하십시오. – Naresh

+0

답변 해 주셔서 감사합니다. 예제 코드를 작성할 수 있습니까? –

+0

https://stackoverflow.com/questions/24049020/nsnotificationcenter-addobserver-in-swift 및 https://stackoverflow.com/questions/30956476/swift-instantiating-a-navigation-controller-without-storyboards-in -app-delegat – Naresh

답변

0

통지 데이터 ...

let str:String = (userInfo["gcm.notification.imType"] as? String)! 

    switch str { 
    case "FIRST": 

     let rootViewController = self.window!.rootViewController as! UINavigationController 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      let firstViewController = storyboard.instantiateViewController(withIdentifier: "FIRSTID") as! MessagesTableViewController 

      rootViewController.pushViewController(firstViewController, animated: true) 

     case "SECOND": 

     let rootViewController = self.window!.rootViewController as! UINavigationController 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let secondViewController = storyboard.instantiateViewController(withIdentifier: "SECONDID") as! SecondViewController 

     rootViewController.pushViewController(secondViewController, animated: true) 
default: 
    print("Type is something else") 

} 
관련 문제