将从AppDelegate的推送通知接收到的数据传递给ViewController

在我的应用程序中,有一个集合视图,显示从Web服务检索到的一组图像。 每个图像都有标签。 所以应用程序也有能力使用标签过滤图像。

现在我试图添加推送通知到这个应用程序。 将新图像添加到服务器时,会发送推送通知。 这些图像被标记为最新的 。 我通过推送通知传递该标记作为消息,我需要的是当用户点击推送通知打开应用程序,它应该加载最新的新图像集合视图。

我完成了一半。 我收到消息成功的推送通知到AppDelegate.m文件中的AppDelegate.m方法。 现在我需要将它传递到集合视图所在的视图控制器。 我被困在这一点上。 我不知道如何将其发送到视图控制器。

我试着在App委托中声明一个属性,给它分配消息值,并从视图控制器中引用它,但是它不起作用。 我绑定代表,通知中心,用户默认,但没有任何工作。

任何人都可以请告诉我如何做到这一点?

谢谢。

编辑:

这是我的代码。 我尝试的最后一个方法是本地通知。

AppDelegate.m

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotificationMessageReceivedNotification" object:nil userInfo:userInfo]; } 

ViewController.m

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remoteNotificationReceived:) name:@"PushNotificationMessageReceivedNotification" object:nil]; } - (void)remoteNotificationReceived:(NSNotification *)notification { NSLog(@"Notification: %@", notification.userInfo); NSString *msg = [[notification.userInfo valueForKey:@"aps"] valueForKey:@"alert"]; self.label.text = msg; } 

案例1:如果您的应用程序是背景,并且用户使用通知单击启动应用程序,则您将检查应用程序是否启动了窗体通知或正常

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; NSDictionary *remoteNotificationPayload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotificationPayload) { [[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:nil userInfo:remoteNotificationPayload]; } return YES; } 

案例2:如果您的应用程序在forground通知将收到didReceiveRemoteNotification

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"userinfo %@",userInfo); [[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:nil userInfo:userInfo]; } 

现在你可以在任何一个带有本地通知的控制器中添加一个观察者,并做你想做的事情

我不知道它会不会起作用,这只是我的build议。 我没有尝试过,但在你的情况下可能是工作用户NSUserDefaults为此。

在你的appDelegate.m

 [[NSUserDefaults standardUserDefaults] setObject:imageArrayFromServer forKey:@"MyAppSpecificGloballyUniqueString"]; 

在你的viewController.m

 NSArray *myArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyAppSpecificGloballyUniqueString"];