postNotificationName is not yet trigger in appdelegate class

993 Views Asked by At

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

5

There are 5 best solutions below

2
Danny Bravo On

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.

4
Jasmeet Kaur On

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];

});
0
David Ansermot On

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.

4
jjpp On

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-->>>");
  } 
1
Santo On

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!");

      }
   }