我的应用程序未运行时如何处理FCM?

时间:2017-08-15 05:33:02

标签: ios swift notifications

我可以在应用处于有效状态或后台时收到FCM。

但是在不跑的时候无法接收。

当应用程序状态未运行时,如何接收FCM?

1 个答案:

答案 0 :(得分:0)

在didFinishLaunchingWithOptions中。有一个参数launchOptions包含推送通知。存储数据并在你打开的firstViewController中处理它。我通过在appDelegate中写这些行来实现它我使用了swiftyJson并将singleton类中的userPush转换为1:

  guard let pushNotification = launchOptions?
[UIApplicationLaunchOptionsKey.remoteNotification]  else { return true }
    print(pushNotification)
    let data = JSON(pushNotification)

    UserDataSingleton.sharedInstance.notificationData = data
    UserDataSingleton.sharedInstance.userPush = 1

现在在我的splashViewController中,我做了:

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    if (UserDataSingleton.sharedInstance.userPush == 1){

        UserDataSingleton.sharedInstance.userPush = 0
        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        appDelegate.handlePushNavigation(userInfo: UserDataSingleton.sharedInstance.notificationData!)

    }
    else{

    }

}

handlePushNotification是我为AppDelegate中的所有3个案例编写的函数:

//MARK: - HAndle Push Notification Methods

extension AppDelegate{

func handlePush(userInfo : JSON){

    handlePushNavigation(userInfo: userInfo )
}

func handlePushNavigation(userInfo :  JSON){

    let storybard = UIStoryboard(name: "Main", bundle: nil)

    switch userInfo["type"].intValue {
    case 1:

        guard let VC = storybard.instantiateViewController(withIdentifier: "ChatViewController") as? ChatViewController else{return}

        navigateToVc(vc: VC)

    default:break

    }

}

func navigateToVc(vc:UIViewController){

    ez.topMostVC?.presentVC(vc)

}

}