应用程序在iOS中接收Apple推送通知时显示警报弹出窗口

时间:2014-09-25 05:39:36

标签: ios ios7 push-notification apple-push-notifications uilocalnotification

我正在使用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}}

谢谢!

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];

}