简体   繁体   English

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

[英]Passing data received from a push notification from the AppDelegate to a ViewController

In my app, there's a collection view displaying a set of images retrieved from a web service. 在我的应用程序中,有一个集合视图,显示从Web服务检索的一组图像。 Each image has tags. 每个图像都有标签。 So the app has the ability to filter images using tags as well. 因此,该应用程序还可以使用标签过滤图像。

Now I'm trying to add push notifications to this app. 现在我正在尝试向此应用添加推送通知。 A push notification is sent when new images have been added to the server. 将新图像添加到服务器时会发送推送通知。 These images are tagged say, latest . 这些图像标记为最新 I'm passing that tag as the message via a push notification and what I need is when the user taps on the push notification to open the app, it should load the latest new images to the collection view. 我通过推送通知将该标记作为消息传递,我需要的是当用户点击推送通知以打开应用程序时,它应该将最新的新图像加载到集合视图中。

I'm half way done. 我已经完成了一半。 I receive the push notification with the message successfully to the didReceiveRemoteNotification method in the AppDelegate.m file. 我收到推送通知与消息成功到didReceiveRemoteNotification的方法AppDelegate.m文件。 Now I need to pass it on to the view controller where the collection view is. 现在我需要将它传递给集合视图所在的视图控制器。 I'm stuck at this point. 我陷入了困境。 I can't figure out how to send it over to the view controller. 我无法弄清楚如何将其发送到视图控制器。

I tried declaring a property in the App delegate, assign the message value to it and referring it from the view controller but it didn't work. 我尝试在App委托中声明一个属性,为它分配消息值并从视图控制器中引用它,但它不起作用。 I tied delegates, notification center, user defaults but nothing worked. 我绑定了代表,通知中心,用户默认值但没有任何效果。

Can anyone please tell me how to accomplish this? 任何人都可以告诉我如何做到这一点?

Thank you. 谢谢。

Edit: 编辑:

Here's my code. 这是我的代码。 The last method I tried was the local notifications. 我尝试的最后一种方法是本地通知。

AppDelegate.m AppDelegate.m

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

ViewController.m 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;
}

Case 1: if your app is background and user launches app with notification click then you have the check if app launched form notification or normal 案例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; }

Case2: If your app is in forground notification will be received in didReceiveRemoteNotification 案例2:如果您的应用程序处于forground通知,则会在didReceiveRemoteNotification中收到

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {


    NSLog(@"userinfo %@",userInfo);

    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:nil userInfo:userInfo];
}

Now you and add a observer in any controller with Local notification and do what you wand to do 现在,您可以在任何带有本地通知的控制器中添加一个观察者,并执行您想要做的事情

I don't know it will work or not,its only my suggestion. 我不知道它会起作用,它只是我的建议。 I did't tried it before but in your case may be it work user NSUserDefaults for this. 我以前没有尝试过,但在你的情况下可能是用户NSUserDefaults为此工作。

in your appDelegate.m 在你的appDelegate.m中

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

in your viewController.m 在你的viewController.m中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM