简体   繁体   English

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

[英]How to open a view controller on click a notification in iOS using Objective c?

I am developing simple app in iOS using objective c. 我正在使用目标c在iOS中开发简单的应用程序。 In my app I added notification. 在我的应用程序中,我添加了通知。 It works fine and notification from server and also appears to the users. 它可以正常工作并从服务器发出通知,并且也向用户显示。 So that I need How to open a view controller on click a notification? 因此,我需要如何在单击通知时打开视图控制器? I'm searching for a method to open a view after tapping on a notification received and display notification on that view to allow the users to read notification information. 我正在寻找一种在点击收到的通知后打开视图并在该视图上显示通知以允许用户读取通知信息的方法。 can any one help me? 谁能帮我?

In your AppDelegate.m class add the below Delegate method 在您的AppDelegate.m类中,添加以下Delegate方法

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

Whenever App gets a notification this delegate method will call, You can handle your logic here. 每当App收到通知时,此委托方法都会调用。您可以在此处处理逻辑。 Below is the simple logic 下面是简单的逻辑

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击目标c中显示的本地通知时,打开特定的视图控制器 - open specific view controller when I click on local notification presented in objective c 收到 iOS 推送通知时如何打开通知视图控制器? - How to Open a Notification view controller when a iOS push notification is received? 使用目标c接收到本地通知数据并传递给主视图控制器后如何创建? - how to create after receiving local notification data pass to Main view controller using objective c? 单击推送通知时打开特定的View Controller - Open Specific View Controller on Click of Push Notification 如何从iOS Objective-C中的控制器重绘自定义视图 - how to redraw custom view from the controller in ios objective-c (Objective-C)打开视图控制器 - (Objective-C) Open view controller 如何在不使用segue概念的情况下在xamarin ios中单击表行上的视图控制器 - How to open a view controller on table row click in xamarin ios without using segue concept 用户单击通知IOS7时如何查看特殊视图控制器 - How to view the Particular view controller when user click the notification IOS7 如何在iOS目标C中处理解析通知? - How to handle parse Notification in ios objective c? 在目标 c 中使用 UIAlertViewController 时如何关闭呈现的视图控制器? - How to dismiss a presented view controller when using UIAlertViewController in objective c?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM