Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中接收到APN时open view controller中出现问题_Ios_Iphone_Apple Push Notifications_Appdelegate - Fatal编程技术网

iOS中接收到APN时open view controller中出现问题

iOS中接收到APN时open view controller中出现问题,ios,iphone,apple-push-notifications,appdelegate,Ios,Iphone,Apple Push Notifications,Appdelegate,嘿,我是iPhone新手,我一直在尝试使用Apple推送通知。基本上,我想做的是,当用户单击收到的推送通知消息时,我需要打开一个特定的视图控制器,它对我有效。我已将带有关键参数“type”的自定义数据添加到我的有效负载JSON中 这是我的有效载荷JSON: {"aps":{"alert":"This is testing message","type":"Notify","badge":1,"sound":"default"}} 这是我的密码: @synthesize viewControll

嘿,我是iPhone新手,我一直在尝试使用Apple推送通知。基本上,我想做的是,当用户单击收到的推送通知消息时,我需要打开一个特定的视图控制器,它对我有效。我已将带有关键参数“type”的自定义数据添加到我的有效负载JSON中

这是我的有效载荷JSON:

{"aps":{"alert":"This is testing message","type":"Notify","badge":1,"sound":"default"}}
这是我的密码:

@synthesize viewControllerNotify;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    screenBounds = [[UIScreen mainScreen] bounds];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if (launchOptions != nil) {
        //Launched from push notification

        NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        NSMutableString *notificationType = [apsInfo objectForKey:@"type"];

        //For redirect to the view
        if([notificationType isEqualToString:@"Notify"]){

            //Notify updates
            UpdatesViewController *uvc1 = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];

            self.viewControllerNotify = uvc1;
        }

        else if([notificationType isEqualToString:@"Voting"] || [notificationType isEqualToString:@"QA"]){

            //Voting & QA
            VotingQAViewController *votingQAViewController = [[VotingQAViewController alloc] initWithNibName:@"VotingQAViewController" bundle:nil];

            self.viewControllerNotify = votingQAViewController;
        }

        else if([notificationType isEqualToString:@"Survey"] || [notificationType isEqualToString:@"Quiz"]){

            //Survey & Quizzes
            SurveysViewController *surveysViewController = [[SurveysViewController alloc] initWithNibName:@"SurveysViewController" bundle:nil];

            self.viewControllerNotify = surveysViewController;
        }

        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewControllerNotify];
        self.window.rootViewController = nav;
        [nav setNavigationBarHidden:YES];

    }
    else{
        //Normal Launch
        if(screenBounds.size.height == 568) {//iPhone5
            splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController_5" bundle:nil];
        }
        else{
            splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
        }

        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:splashViewController];
        self.window.rootViewController = nav;
        [nav setNavigationBarHidden:YES];

    }

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSDictionary *aps = (NSDictionary *)[userInfo objectForKey:@"aps"];
    int badge = [[aps objectForKey:@"badge"] intValue];
    NSMutableString *notificationType = [aps objectForKey:@"type"];

    NSLog(@"Number of badge is = %d", badge);
    NSLog(@"notification type is = %@", notificationType);

    //For redirect to the view
    if([notificationType isEqualToString:@"Notify"]){

        //Notify updates
        UpdatesViewController *uvc1 = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];
        self.viewControllerNotify = uvc1;
    }

    else if([notificationType isEqualToString:@"Voting"] || [notificationType isEqualToString:@"QA"]){

        //Voting & QA
        VotingQAViewController *votingQAViewController = [[VotingQAViewController alloc] initWithNibName:@"VotingQAViewController" bundle:nil];

        self.viewControllerNotify = votingQAViewController;
    }

    else if([notificationType isEqualToString:@"Survey"] || [notificationType isEqualToString:@"Quiz"]){

        //Survey & Quizzes
        SurveysViewController *surveysViewController = [[SurveysViewController alloc] initWithNibName:@"SurveysViewController" bundle:nil];

        self.viewControllerNotify = surveysViewController;
    }

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewControllerNotify];
    self.window.rootViewController = nav;
    [nav setNavigationBarHidden:YES];
    [self.window makeKeyAndVisible];
}
我的问题是:当收到推送通知时,这是重定向到视图控制器的正确方法吗


使用上面的代码,它代表Notification type(Notification payload json的自定义键)在forground和background模式下成功重定向到view controller类,但重定向的view controller back按钮不起作用。我不知道我哪里做错了。如果有人知道,请帮助我。谢谢。

后退按钮不起作用,因为您正在将目标视图设置为根视图。相反,您应该构建完整的导航堆栈,也就是说,通常,如果您有一个导航视图控制器作为根视图控制器,那么您应该首先创建它,然后构建根视图和叶视图之间的所有视图控制器

例如,如果MainViewController是您的正常开始视图,并且UpdatesViewController中的“后退”按钮应指向MainViewController,则您可以执行以下操作:

MainViewController *mainVC = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
UpdatesViewController *updatesVC = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil];

UINavigationController *navVC=[[UINavigationController alloc]initWithRootViewController:mainVC];      
[navVC setViewControllers:@[mainVC, updatesVC] animated:NO];

self.window.rootViewController = navVC;

您最好使用
didReceiveMemotentification
中的
nsNotificationCenter
发布通知,并在其他类上接收通知。以便您可以从当前视图控制器推送所需的视图控制器(无需设置为根视图控制器)。然后“后退”按钮将工作

例如

[[NSNotificationCenter defaultCenter] postNotificationName:kMesssagePushNotification object:nil userInfo:userInfo];

您有两种机会访问通知数据

  • 如果您在应用未打开时收到通知,则单击该通知,您将在以下功能中获得通知数据:
  • 访问通知数据并打开所需的视图控制器

  • 如果您在应用程序打开时收到通知,但您的应用程序可以位于后台或前台。如果是前一种情况,您将在通知中心收到通知,您的应用程序将在您单击通知后调用以下功能:

  • 对不起,我的池英语,希望它能帮助~

    你在故事板中使用的我正在为我的应用程序中的每个视图控制器使用XIB。感谢你的回复..我将如何为视图控制器设置DidReceiveMemoteNotification条件。您是否要求从AppDelegate委派给其他视图控制器?只需通过
    NSNotificationCenter
    传递
    userInfo
    字典,并检查条件以查看我的更新答案。@冲浪者感谢您的建议,我已将导航视图控制器作为根视图,但问题仍然相同。我正在更新我的问题..请check@TeemuThanks对于您的建议,我已经将导航视图控制器作为根视图,但问题还是一样的。我正在更新我的问题..请check@user2786我编辑了“回答”以显示将多个视图按到导航控制器以使“后退”按钮正常工作。@TeemuI已使用编辑的代码进行了检查。。但不起作用。我已经尝试了很多解决方案。你能发布准确的代码并解释你试图实现的导航结构吗?(例如,如果UpdatesVC被打开,它的后退按钮应该指向哪个视图?@teemmy应用程序有1。Splash VC,2。登录VC和3,主VC。收到通知后,我需要重定向三个不同的VC,即更新VC、VotingQA VC和调查VC,具体取决于通知类型。如果用户将点击所有重定向VC中的后退按钮,那么它将转到主VC。这是我的应用程序的主主题。。。我已经在编辑的问题中向您发送了应用程序代理的相关代码。非常感谢你给我时间。请找出我的错误,这样我就可以继续。请随时询问关于我问题的任何疑问。 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
    
    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    if (state == UIApplicationStateBackground || state == UIApplicationStateInactive){
        //notification is received when your app is in background
        //open the view controller you expected
    }else if(state == UIApplicationStateActive){
        //notification is received when your app is in foreground
        //do nothing
    }