1

In Appdelegate.m added postNotification method

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

         if(application.applicationState == UIApplicationStateBackground) {

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


              AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];

                        SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init];
                        MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                                        containerWithCenterViewController:[[UINavigationController alloc]
                                                                                                           initWithRootViewController:[[SuggetionViewController alloc] init]]
                                                                        leftMenuViewController:leftMenuViewController
                                                                        rightMenuViewController:Nil];

                         [self.window makeKeyAndVisible];
                        appDel.window.rootViewController = container;

                 }


                    }

ViewController B (SuggetionViewController) In viewDidLoad

   [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveTestNotification:)
                                                 name:@"SuggetionPushNotification"
                                               object:nil];



- (void) receiveTestNotification:(NSNotification *) notification {
    NSLog(@"working");

}

But here not yet fire Notification, if added both post and addobserver in same class then only it fire. what wrong i made. I referred from Send and receive messages through NSNotificationCenter in Objective-C? Please help

Community
  • 1
  • 1
Joyal Clifford
  • 1,212
  • 11
  • 20

5 Answers5

2

Your View Controller B is not in memory when you are posting the notification thats why Controller B is unable to observe the notification. Add Delay before posting the notification will help.

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  [[NSNotificationCenter defaultCenter] postNotificationName: @"SuggetionPushNotification" object:nil userInfo:nil];

});
Jasmeet Kaur
  • 1,538
  • 14
  • 16
0

You're calling your notification from the launch method in the app delegate before any view/viewcontroller hierarchy has a change to instantiate and load. The reason you are not getting the notification is because your SuggetionViewController has not been instantiated yet. If you want the notification to be received, you need to fire it after the view controller gets a change to be created.

Danny Bravo
  • 4,534
  • 1
  • 25
  • 43
0

You can't receive notification inside your SuggetionViewController if it's now loaded.
You add the observer in the viewDidLoad so it can happens that you recieve a remote notification while the SuggetionViewController is not loaded.

You should ensure that your controller is loaded before processing notifications.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
0

Your 'SuggetionViewController' object is not initialized when you post the notification. So there is no 'SuggetionViewController' object to catch the notification.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
if(application.applicationState == UIApplicationStateBackground) {

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init];
    SuggetionViewController *sug_controller = [[SuggetionViewController alloc] init]];
    [sug_controller registerNotification];

    [[NSNotificationCenter defaultCenter] postNotificationName: @"SuggetionPushNotification" object:nil userInfo:userInfo];
    MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController containerWithCenterViewController:[[UINavigationController alloc] initWithRootViewController:sug_controller leftMenuViewController:leftMenuViewController rightMenuViewController:Nil];
    [self.window makeKeyAndVisible];
    appDel.window.rootViewController = container;
}
}

Add the following instance method to SuggetionViewController

- (void)registerNotification
 {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"SuggetionPushNotification" object:nil];
 }

- (void)receiveTestNotification
  {
        NSlog(@"Notification recieved-->>>");
  } 
jjpp
  • 1,298
  • 1
  • 15
  • 31
  • So if i did SuggetionViewController *newObject=[SuggetionViewController new]; – Joyal Clifford May 18 '16 at 12:26
  • if i did also in appdelegate it will not fire post – Joyal Clifford May 18 '16 at 12:27
  • ViewDidload method will not get called right after the object allocation. Please fire the notification from appdelegate using a timer after a delay(1 or 2 secs) – jjpp May 18 '16 at 12:31
  • @JoyalClifford You can try the above code. Before adding any code inside receiveTestNotification method just check if the code working by adding a log inside the method. – jjpp May 18 '16 at 12:57
0

In your AppDelegate implementation add this

static void displayStatusChanged(CFNotificationCenterRef center, void *observer,
                             CFStringRef name, const void *object,
                             CFDictionaryRef userInfo) {
if (name == CFSTR("com.apple.springboard.lockcomplete")) {
    [[NSUserDefaults standardUserDefaults] setBool:YES
                                            forKey:@"kDisplayStatusLocked"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
}

In applicationDidFinishLaunchingWithOptions, add this below code

CFNotificationCenterAddObserver(
                                CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged,
                                CFSTR("com.apple.springboard.lockcomplete"), NULL,
                                CFNotificationSuspensionBehaviorDeliverImmediately);

Post it in your didRecieveRemoteNotification method.

     [[NSNotificationCenter defaultCenter]postNotificationName:@"TestNotification" object:self];

Post this in your view controller viewDidLoad method.

     [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveTestNotification:)
                                             name:@"TestNotification"
                                           object:nil];

Then it will trigger this method.

 - (void) receiveTestNotification:(NSNotification *) notification
   {

    if ([[notification name] isEqualToString:@"TestNotification"]) {
    NSLog (@"Successfully received the test notification!");

      }
   }
Santo
  • 1,611
  • 3
  • 17
  • 37
  • @JoyalClifford please try the above code once, have edited and checked it is working fine, hope it will help you... – Santo May 18 '16 at 12:39