1

在 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) 在 vi​​ewDidLoad

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



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

}

但是这里还没有触发通知,如果在同一个类中添加了 post 和 addobserver,那么只有它会触发。我做错了什么。我在 Objective-C 中通过 NSNotificationCenter 发送和接收消息? 请帮忙

4

5 回答 5

2

当您发布通知时,您的视图控制器 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];

});
于 2016-05-18T09:44:32.633 回答
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!");

      }
   }
于 2016-05-18T12:26:13.483 回答
0

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

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

于 2016-05-18T11:51:57.693 回答
0

在任何视图/视图控制器层次结构发生更改以实例化和加载之前,您正在从应用程序委托中的启动方法调用通知。您没有收到通知的原因是您的 SuuggetionViewController 尚未实例化。如果您希望收到通知,则需要在视图控制器获得要创建的更改后触发它。

于 2016-05-18T09:42:09.070 回答
0

发布通知时,您的“建议视图控制器”对象未初始化。所以没有“SuggerionViewController”对象来捕捉通知。

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

将以下实例方法添加到 SuuggetionViewController

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

- (void)receiveTestNotification
  {
        NSlog(@"Notification recieved-->>>");
  } 
于 2016-05-18T12:23:54.777 回答