[英]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.