postNotificationName尚未在appdelegate类中触发

时间:2016-05-18 09:36:28

标签: ios objective-c push-notification nsnotificationcenter mfslidemenu

在Appdelegate.m中添加了postNotification方法

   - (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)在viewDidLoad

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



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

}

但是这里还没有消息通知,如果在同一个类中添加了post和addobserver,那么只有它才会触发。我做错了什么。我提到Send and receive messages through NSNotificationCenter in Objective-C?请帮忙

5 个答案:

答案 0 :(得分:2)

当您发布通知时,您的View Controller B不在内存中,这就是控制器B无法观察通知的原因。在发布通知之前添加延迟将有所帮助。

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

});

答案 1 :(得分:0)

在任何视图/视图控制器层次结构进行实例化和加载更改之前,您将从应用委托中的启动方法调用通知。您没有收到通知的原因是因为您的SuggetionViewController尚未实例化。如果要接收通知,则需要在视图控制器获取要创建的更改后触发通知。

答案 2 :(得分:0)

如果您的SuggetionViewController已加载,则无法收到通知 您可以在viewDidLoad中添加观察者,以便在未加载SuggetionViewController时收到远程通知。

在处理通知之前,您应确保已加载控制器。

答案 3 :(得分:0)

发布通知时,您的'SuggetionViewController'对象未初始化。所以没有'SuggetionViewController'对象来捕获通知。

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

将以下实例方法添加到SuggetionViewController

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

- (void)receiveTestNotification
  {
        NSlog(@"Notification recieved-->>>");
  } 

答案 4 :(得分:0)

在AppDelegate实现中添加此

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

在applicationDidFinishLaunchingWithOptions中,添加以下代码

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

在didRecieveRemoteNotification方法中发布它。

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

在视图控制器viewDidLoad方法中发布此内容。

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

然后它将触发此方法。

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

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

      }
   }