ios - 当用户点击通知 IOS7 时导航到特定 View Controller

标签 ios iphone objective-c notifications

您好,我在我的应用程序中添加了推送通知,我想在用户点击通知时查看特定的 ViewController。在我的应用程序委托(delegate) m 文件中,我尝试将设备 token 注册到我的服务器,并从我的服务器使用 php 脚本从服务器获取设备 token ,然后发送通知。

这里的问题是,当用户点击通知时,我试图查看特定的 View Controller ,但它不起作用,我尝试了许多不同的方法,但没有任何效果。

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

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


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

在这里,我将设备存储为 token 到我的服务器。

-(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];
       }

我的特定 View Controller 名称是 updatepoliticalViewController 它是一个导航 View Controller ,请告诉我在上面的代码中我做错了什么,如何解决此问题。

谢谢

最佳答案

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

application:didReceiveRemoteNotification:

但如果不是,并且应用程序已启动,例如,通过在通知中心滑动警报

application:didFinishLaunchingWithOptions:

用键调用。 好的方法是调用

application:didReceiveRemoteNotification:

来自

application:didFinishLaunchingWithOptions:

我相信这应该有帮助

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

//扩展

尝试获取这样的 Storyboard:

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

从 mainstoryboard 创建对象:

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

并尝试设置新的 Root View Controller

[self.window setRootViewController: ringingVC];

关于ios - 当用户点击通知 IOS7 时导航到特定 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607248/

相关文章:

ios - 如何使用 Stripe 的 Apple Pay

ios - CSS 动画阻止了 iOS 上的触摸事件

iphone:如何检查 uitableview 行模数

ios - 使用 alamofire 的 token 请求

iphone - 更改现有 iPhone/iPad 应用程序上的目标设备系列

iphone - UITextView 子类中的 viewDidLoad?

ios - 如何仅以英文获取 Googleapis 响应?

objective-c - 参数是 KindOfClass : [NSNumber class] - sane way to check this?

iOS - 本地化 Storyboard Scene/ViewController 的最佳架构

objective-c - App Store - Method Swizzling 合法性