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

Isu*_*uru 9 image push-notification apple-push-notifications viewcontroller ios

在我的应用程序中,有一个集合视图,显示从Web服务检索的一组图像.每个图像都有标签.因此,该应用程序还可以使用标签过滤图像.

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

我已经完成了一半.我收到推送通知,并将消息成功发送到文件中的didReceiveRemoteNotification方法AppDelegate.m.现在我需要将它传递给集合视图所在的视图控制器.我陷入了困境.我无法弄清楚如何将其发送到视图控制器.

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

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

谢谢.

编辑:

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

AppDelegate.m

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

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;
}
Run Code Online (Sandbox Code Playgroud)

mor*_*oko 0

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

在你的 appDelegate.m 中

[[NSUserDefaults standardUserDefaults] setObject:imageArrayFromServer forKey:@"MyAppSpecificGloballyUniqueString"];
Run Code Online (Sandbox Code Playgroud)

在你的 viewController.m 中

NSArray *myArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyAppSpecificGloballyUniqueString"];
Run Code Online (Sandbox Code Playgroud)