My aim is writing a code that when a user gets a push notification, I want that user to be redirected to another view. If the user comes with the push notification and if he has been first viewed the controller (welcome home screen etc. (but not logged in))
var rootViewController = self.window!.rootViewController as! ViewController
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
This couple lines of code is working, however, if the user has been in another view controller (sign in/login/user page etc.) this piece of code doesn't work and redirect. I tried everything and I still cannot come up with a solution. My final goal is this:
if let rootViewController = self.window!.rootViewController as? ViewController
{
var rootView: UserViewController = UserViewController()
if let window = self.window{
window.rootViewController = rootView
}
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
println(self.window?.rootViewController)
}
Can anyone give me an idea?
The answer code is Objective-C, I want to talk about logic. To do this, when you are creating your push notification, you must set userInfo value to pass data with your push notification.
Use this delegate method : application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
You can decide if which view you want to show by handling userInfo
NSDictionary *segueDictionary = [userInfo valueForKey:@"aps"];
NSString *segueName=[[NSString alloc]initWithFormat:@"%@",[segueDictionary valueForKey:@"segueName"]];
if([segueName isEqualToString:@"hospitalSegue"])
{
// implement your code to redirect
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With