Swift - How to open specific view controller when push notification received?
When you app is in closed state you should check for launch option in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { }
and call your API.
Example:
if let option = launchOptions {
let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
if (info != nil) {
goAnotherVC()
}
}
Unable to navigate to specific viewController after clicking push notification in foreground using FCM below iOS 10
Implement below extension of UIApplication
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
Please change your code to navigate to specific screen while app is in foreground.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "chatMessageVC") as! ChatMessagesVC
UIApplication.topViewController()?.navigationController?.pushViewController(viewController, animated: false)
Hope this finds you well and let me know in case of any queries.
UPDATE
If the application is running in the foreground, iOS won't show a notification banner/alert. That's by design. But we can achieve it by using UILocalNotification as follows
if application.applicationState == .active {
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
}
Programmatically navigate to another view controller/scene
I already found the answer
Swift 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
Load specific View after push notifications
You can try below approch:
Add this method in the AppDelegate Class:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Open Your Details view controller directly using segue
// You can pass details id and then fetch details and display in the view.
SettingViewController *moreVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
UINavigationController *navigationRootController = [[UINavigationController alloc] initWithRootViewController:moreVC];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:navigationRootController animated:YES completion:NULL];
}
This is another approch:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Register the below observer in the rootviewcontroller so its registered first and than Pass NSDictionary
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushModalPopUp" object:userInfo];
}
iOS Swift: Pushing two view controllers when app launched from push notification
Can you try changing this
homeNav.addChildViewController(homePageTableVC)
to this
homeNav.setViewControllers([homePageTableVC], animated: false)
Related Topics
How to Get Email Id of User Using Facebook Sdk 4.7 in iOS 9
Uitableviewcell Auto Height Based on Amount of Uilabel Text
Detect Touch on Child Node of Object in Spritekit
How to Clear or Clean Specific Pod from the Local Cocoapods Cache
Flutter: Cocoapods's Specs Repository Is Too Out-Of-Date to Satisfy Dependencies
Alamofire: Finished with Error - Code: -1001
Doing Undo and Redo with Cglayer Drawing
How Can Split from String to Array by Chunks of Given Size
Reading Text and Images from a PDF Document in iOS
Parsing JSON Within JSON in Objective-C
Integrate Google Contacts API into My Swift 3 App
Alamofire Fire Variable Type Has No Subscript Members
How to Install an Unsigned IPA File on My Device Using an Apple Developer Account