Passing data received from a push notification from the AppDelegate to a ViewController
Asked Answered
S

2

9

In my app, there's a collection view displaying a set of images retrieved from a web service. 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. 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. 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

- (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;
}
Seve answered 10/3, 2014 at 12:28 Comment(8)
you and post a new local notification and pass the dictionary in notification objectRollins
Tried that but it only worked if I have the app running in the foreground. If its in the background, it didn't work.Seve
what is the problem, when you are declaring a property in app delegate. What is the error.Sorbian
do you develop for iOS 7 ?Shaven
@rajath No error thrown. The value gets assigned to the property correctly in the didReceiveRemoteNotification method. I had the retrieving code in the viewWillAppear method of the view controller. No value is received from that end.Seve
@Shaven Yes, that right.Seve
CAn you show your code of setting and getting values...Sorbian
How have you initiated ViewController in your app? We can catch the notification in didFinishLaunchingWithOptions method (in case app starts when killed and gets the notification) and pass the tag to the ViewController.Sauerkraut
R
4

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

    - (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

- (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

Rollins answered 10/3, 2014 at 12:48 Comment(7)
Case 2 works fine. Case 1 doesn't for some reason. I post the same notification from the didFinishLaunchingWithOptions as well but nothing happens.Seve
@Seve is your app killed ?Rollins
@Seve other way is in didFinishLaunchingWithOptions you check the notification and push the viewcontroller with notificationpayload. This should workRollins
You're right. I tried both ways. When the app is in the background and not killed and when the app is completely terminated. It worked when the app was alive but in the background. Didn't work when the app was killed. Any idea how to do this when the app is completely terminated too?Seve
when its terminated, did you launched app by clicking notification?Rollins
Yes, I opened the app by clicking the notification.Seve
let us continue this discussion in chatRollins
L
0

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.

in your appDelegate.m

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

in your viewController.m

NSArray *myArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyAppSpecificGloballyUniqueString"];
Lilialiliaceous answered 10/3, 2014 at 12:40 Comment(2)
I tried exactly this. But no value returned. In the viewWillDisappear of the view controller, I tried retrieving the value but no luckSeve
hey i am getting value from this i create an imageArray in applicationDidEnterBackground in appdelegate, and getting them in viewcontrollerLilialiliaceous

© 2022 - 2024 — McMap. All rights reserved.