How can I handle FCM when my app is not running?

Home / Uncategorized / How can I handle FCM when my app is not running?

Question:
I can receive FCM when app is active or background.

But can’t receive when not running.

How can I receive FCM when application state not running?


Answer:
in didFinishLaunchingWithOptions.There is a parameter launchOptions which contains push notification.Store the data and handle it in firstViewController you open.I did it by writing these lines in appDelegate I used swiftyJson and take userPush in singleton class to 1:guard let pushNotification = launchOptions?
[UIApplicationLaunchOptionsKey.remoteNotification] else { return true }
print(pushNotification)
let data = JSON(pushNotification)

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

Now in my splashViewController I did: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 is a func i have written for all 3 cases in AppDelegate://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)

}

}
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *