我可以在应用处于有效状态或后台时收到FCM。
但是在不跑的时候无法接收。
当应用程序状态未运行时,如何接收FCM?
答案 0 :(得分:0)
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)
}
}