简体   繁体   中英

ios push notifications to a specific view controller

My app receives remote push notifications and upon receiving them, i would like to take it to a specific view controller(pretty much all the time). I have seen code like this (on didReceiveRemoteNotification). I am using storyboard for my apps.

    UIViewController *vc = self.window.rootViewController;
    PushViewController *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"someid"];
    [vc presentViewController:pvc animated:YES completion:nil];

This seems straightforward enough. But will it have any impact on memory allocation, as it looks like every time a push notification arrives, we are instantiating a new view controller. Is this the best practice?

you can try something like:

    PushViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"someid"];

   [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];

You dont need to allocate uiviewcontroller as rootview everytime unless you want to have a uiviewcontroller present your pushVC.

if you are doing this in app delegate, the get the storyboard reference by:

  UIStoryboard *test=[UIStoryboard storyboardWithName:@"name" bundle:nil];

and then call

  [test instantiateViewControllerWithIdentifier.....

The above code you have mentioned is fine. But the logic remains upto you on how to handle. If it is intended that you always want different VC's for your push notification, then it is fine.

Say if you get two push notification instantly where two VC's will be presented immediately. But you want just one screen for the Push then this will be in trouble.

If you want to have one pushViewController object then you can make it a global object and do a simple check like this

UIViewController *vc = self.window.rootViewController;
if(!pvc)
{
   pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"someid"];
}
[vc updatePushViewControllerDependingOnPushNotificationObject:somePushObject];
[vc presentViewController:pvc animated:YES completion:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM