2017-05-19 233 views
0

我剛剛用swift 3編寫了我的第一個應用程序.Firebase運行良好,並且我在我的設備上接收通知。我想要做的是在用戶單擊通知時設置操作。我想發送到特定的ProductViewController,具有特定的產品ID(從後端發送)。如果用戶被鎖定到ProductViewController,則不保留在登錄屏幕上。Firebase推送通知操作

AppDelegate.swift代碼:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate { 

var window: UIWindow? 

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
    print(remoteMessage.appData) 
} 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    // Override point for customization after application launch. 

    let lagFreeField = UITextField() 
    self.window?.addSubview(lagFreeField) 
    lagFreeField.becomeFirstResponder() 
    lagFreeField.resignFirstResponder() 
    lagFreeField.removeFromSuperview() 


    // [START register_for_notifications] 
    if #available(iOS 10.0, *) { 
     let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound] 
     UNUserNotificationCenter.current().requestAuthorization(
      options: authOptions, completionHandler: {_,_ in }) 

     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate 
     // For iOS 10 data message (sent via FCM) 
     FIRMessaging.messaging().remoteMessageDelegate = self as? FIRMessagingDelegate 

    } else { 
     let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil) 

     application.registerUserNotificationSettings(settings) 
     application.registerForRemoteNotifications() 
    } 

    application.registerForRemoteNotifications() 

    // [END register_for_notifications] 

    FIRApp.configure() 

    print("AppDelegate") 

    IQKeyboardManager.sharedManager().enable = true 
    self.window = UIWindow(frame: UIScreen.main.bounds) 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let tab = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController 
    self.window?.rootViewController = tab 
    if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true{ 
     APIRequest.username = Defaults[.username]! 
     APIRequest.password = Defaults[.password]! 
     let tab = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController 
     self.window?.rootViewController = tab 

    } else { 
     let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController") 
     self.window?.rootViewController = controller 
    } 
    self.window?.makeKeyAndVisible() 

    return true 
} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 
    print("Userinfo \(userInfo)") 
} ... other default functions 

回答

0

你需要用火要開的viewController的標識符SEGUE ..

self.performSegue(withIdentifier: "ProductViewControllerSegue",sender: nil) 

您還需要通過你的故事板設置SEGUE爲了實現這一點..

+0

我知道這一點,但我在哪裏可以添加這行代碼,當我按通知執行此segue ..? – DocPllana

+0

您可以發送通知使用notificationCenter來寫入您的主視圖控制器中的函數,該函數可以執行此行.. –