Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect a View with Push Notification in AppDelegate

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?

like image 609
Rys Avatar asked Nov 09 '22 12:11

Rys


1 Answers

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
}
like image 76
Mustafa Sait Demirci Avatar answered Nov 14 '22 22:11

Mustafa Sait Demirci