我正在使用Chat
创建Apple push notification services
种iPhone应用程序。 APN的工作正常,当用户收到新消息时我收到notification
。所以,我在我的Toast
课程的didReceiveRemoteNotification
中设置了一个App Delegate
。问题是,我在每个View Controller
屏幕中弹出Toast,因为我已经在main window
本身添加了Toast。但是你可以帮助我,我怎么能隐藏Toast
我的Chat List View Controller
屏幕中的View Controller
弹出窗口。当应用程序位于foreground
时,如何检查当前在我的窗口视图中加载的- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
if ( application.applicationState == UIApplicationStateActive ){
NSString *personName = [[userInfo valueForKey:@"aps"] valueForKey:@"user_name"];
NSString *meassge = [NSString stringWithFormat:@"New message from %@.", personName];
[[self window] makeToast:meassge duration:1.0 position:@"center"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
}
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
?
这是我的代码:
{{1}}
谢谢!
答案 0 :(得分:0)
在AppDelegate.m
中声明以下方法 - (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
if ( application.applicationState == UIApplicationStateActive ){
NSString *personName = [[userInfo valueForKey:@"aps"] valueForKey:@"user_name"];
NSString *meassge = [NSString stringWithFormat:@"New message from %@.", personName];
if(![[self topViewController] isKindOfClass:[ChatListViewController class]])//ChatListViewController is your viewcontroller
[[self window] makeToast:meassge duration:1.0 position:@"center"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
}
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}