当用户点击通知IOS7时,导航到特定的视图控制器

时间:2014-03-24 10:50:17

标签: ios iphone objective-c notifications

您好我在我的应用程序中添加了推送通知,并且我想在用户点击通知时查看特定的ViewControllers。在我的app delegate m文件中,我试图将设备令牌注册到我的服务器,并从我的服务器上我使用php脚本从服务器获取设备令牌并且我发送了通知。

这里的问题我试图查看特定的视图控制器,当用户点击通知它不工作时我尝试了许多不同的方法没有用。

我在这里查看弹出窗口,以便在用户第一次尝试安装应用程序时从应用程序发送通知。

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


- (void) clearNotifications {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
 }

这里我将设备存储到我的服务器。

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
 {

    const char* data = [deviceToken bytes];
    NSMutableString * token = [NSMutableString string];

   for (int i = 0; i < [deviceToken length]; i++) {
      [token appendFormat:@"%02.2hhX", data[i]];
 }


     NSString *urlString = [NSString stringWithFormat:@"url?token=%@",token];

     NSURL *url = [[NSURL alloc] initWithString:urlString];
     NSLog(@"token %@",urlString);


     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
     NSLog(@"request %@ ",urlRequest);
     NSData *urlData;
     NSURLResponse *response;
     urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];    
     NSLog(@"data %@",urlData);
     [self clearNotifications];
     //  NSLog(@"token ",sendUserToken);

    }

我尝试在用户点按通知时查看特定方法。

   -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
           updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];
           [self.window.rootViewController presentViewController:ringingVC animated:YES completion:NULL];
       }

我的特定视图控制器名称是updatepoliticalViewController它是导航视图控制器,请在上面的代码中告诉我我在哪里做错了如何解决此问题。

由于

2 个答案:

答案 0 :(得分:3)

当应用程序处于前台状态时,它会调用

  

应用:didReceiveRemoteNotification:

但如果不是,则启动应用程序,例如,通过在通知中心中滑动提醒

  

应用中:didFinishLaunchingWithOptions:

用密钥调用

。 好的方法是打电话

  

应用:didReceiveRemoteNotification:

来自

  

应用中:didFinishLaunchingWithOptions:

我相信这应该有帮助

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
    }
    // YOUR CODE...
    return YES;
}

// EXTENDED

尝试获取这样的故事板:

// Make sure the name match
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

从mainstoryboard创建对象:

updatepoliticalViewController *ringingVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"updatepoliticalViewController"];

尝试设置新的根视图控制器

[self.window setRootViewController: ringingVC];

答案 1 :(得分:0)

1) 当应用程序在后台运行时当应用程序在前台运行时 application:didReceiveRemoteNotification:方法将在下面调用。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
     {
     //opened from a push notification when the app was on background

       NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
     }
  else if(application.applicationState == UIApplicationStateActive)
     {
     // a push notification when the app is running. So that you can display an alert and push in any view

        NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
     }
}

2) 当应用程序未启动(关闭)时,将调用application:didFinishedLaunchWithOptions方法。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if (launchOptions != nil)
    {
         //opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
             NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
        }

    }
     else{
             //opened app without a push notification.
         }
}