В вашем классе AppDelegate.m добавьте приведенный ниже метод Delegate
-(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;
}
}
}