Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.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/5/objective-c/22.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
如何使用Objective c在iOS中单击通知时打开视图控制器?_Ios_Objective C_Notifications - Fatal编程技术网

如何使用Objective c在iOS中单击通知时打开视图控制器?

如何使用Objective c在iOS中单击通知时打开视图控制器?,ios,objective-c,notifications,Ios,Objective C,Notifications,我正在使用objective c在iOS中开发简单的应用程序。在我的应用程序中,我添加了通知。它工作正常,可以从服务器发出通知,也可以显示给用户。所以我需要如何在单击通知时打开视图控制器? 我正在搜索一种方法,在点击收到的通知后打开一个视图,并在该视图上显示通知,以允许用户阅读通知信息。 有人能帮我吗?在AppDelegate.m类中添加以下委托方法 -(void)application:(UIApplication *)application didReceiveRemoteNotificat

我正在使用objective c在iOS中开发简单的应用程序。在我的应用程序中,我添加了通知。它工作正常,可以从服务器发出通知,也可以显示给用户。所以我需要如何在单击通知时打开视图控制器? 我正在搜索一种方法,在点击收到的通知后打开一个视图,并在该视图上显示通知,以允许用户阅读通知信息。
有人能帮我吗?

在AppDelegate.m类中添加以下委托方法

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
每当应用程序收到此委托方法将调用的通知时,您都可以在此处处理您的逻辑。下面是简单的逻辑

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    NSLog(@"didReceiveRemoteNotification with completionHandler");
    // Must call completion handler
    if (userInfo.count > 0) {
        completionHandler(UIBackgroundFetchResultNewData);
    } else {
        completionHandler(UIBackgroundFetchResultNoData);
    }
    NSLog(@"userInfo:%@", userInfo);    
    __weak typeof (self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        SEL openDetails = @selector(openDetailsViewFromNotificationInfo:);
        //The below line will removes previous request.
        [NSObject cancelPreviousPerformRequestsWithTarget:strongSelf selector:openDetails object:userInfo];
        //Not neccessary 
        [strongSelf performSelector:openDetails withObject:userInfo afterDelay:0.5];
    });

}
-(void)openDetailsViewFromNotificationInfo:(NSDictionary *)userInfo {

    UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
    UIViewController *topVC = navVC.topViewController;
    NSLog(@"topVC: %@", topVC);
    //Here BaseViewController is the root view, this will initiate on App launch also.
    if ([topVC isKindOfClass:[BaseViewController class]]) {
        BaseViewController *baseVC = (BaseViewController *)topVC;
        if ([baseVC isKindOfClass:[YourHomeVC class]]) {
            YourHomeVC *homeVC = (YourHomeVC *)baseVC;
            homeVC.notificationUserInfo = userInfo;
        }
    }
}

你好,svs先生,homeVC中的notificationUserInfo是什么。notificationUserInfo=userInfo@Mohamed Ali,notificationUserInfo是一个通用变量,我们可以将推送通知元数据传递给所需的类。