用户单击通知IOS7时如何查看特定视图控制器

时间:2014-03-24 06:38:29

标签: ios objective-c uiviewcontroller apple-push-notifications

我在我的应用程序中添加了推送通知,我希望在用户启用PushNotifications时查看特定的ViewControllers,但是只有我尝试了一些不起作用的方法才能进入主视图控制器。请告诉我如何解决这个问题。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];
    [self.window.rootViewController presentViewController:ringingVC animated:YES completion:NULL];
 }

我的didFinishMethod:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
        return YES;
  }

我的视图控制器名称是updatepoliticalViewController这是视图控制器,我想在用户点击updatepoliticalViewController中使用导航视图控制器的通知时去。请告诉我哪里做错了。

感谢。

3 个答案:

答案 0 :(得分:0)

编辑Maintain flag中的AppDelegate即创建bool isNotified;并为其property;

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

   //need validation then add here as such these notification should only open new view controller
   self.isNotified = TRUE;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  if(self.isNotified)
  {
     //add your viewcontroller here to UIWindow
  }
  self.isNotified = NO;

}

答案 1 :(得分:0)

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    NSLog(@"notification Alertbody message:::%@",notif.alertBody);


    if (app.applicationState == UIApplicationStateInactive)
    {
        viewcontroller *slideViewController = [[viewcontroller alloc] initWithNibName:@"viewcontroller" bundle:nil];
        UINavigationController *navl=[[UINavigationController alloc] initWithRootViewController:slideViewController];
        self.window.rootViewController=navl;
        [slideViewController release];
    }
    else if (app.applicationState == UIApplicationStateActive)
    {
        NSLog(@"notification Alertbody message:::%@",notif.alertBody);

    }

}

答案 2 :(得分:0)

我想你想要一个didReceiveRemoteNotification而不是didReceiveLocalNotification

试试这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
   //...

   NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
   if(userInfo){
      //Do something with userInfo for notification, in this case app is closed when receive remote notification.
      updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];
[self.window.rootViewController presentViewController:ringingVC animated:YES completion:NULL];

   }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
     if(application.applicationState != UIApplicationStateActive){
       //Do something with userInfo for notification, in this case app is in background when receive remote notification.
       updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];
[self.window.rootViewController presentViewController:ringingVC animated:YES completion:NULL];
     }
}