Obj- C - NSNotification не выполняется? - PullRequest
0 голосов
/ 25 января 2020

Я пытаюсь выполнить серию уведомлений, пока мое приложение работает в фоновом режиме. Короче говоря, когда уведомление получено, я хочу, чтобы выполнялись следующие уведомления:

pushNotification , bellAlert

При этом bellAlert должно выполняться изнутри pushNotification . Теперь, когда я запускаю этот тест, pushNotification выполняется так, как должно, и также похоже, что bellAlert выполняется (журнал «BELL ALERT TRIGGERED» появляется в консоли, как и должно). Однако даже если журнал возвращается, как если бы BellAlert был выполнен, все, что закодировано внутри BellAlert, не работает при выполнении (например, даже журнал внутри BellAlert - «Красный круг вызван» не возвращается).

Есть идеи, почему код bellAlert не выполняется?

Приветствия, см. Код ниже:

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{


    if (application.applicationState == UIApplicationStateBackground) {

 static int i=1;
  [UIApplication sharedApplication].applicationIconBadgeNumber = i++;

        NSLog(@"application Background - notification has arrived when app was in background");
        NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]];


        if([contentAvailable containsString:@"New Message"] || ![contentAvailable containsString:@"New Message"] || ![contentAvailable isEqualToString:@"You have a new friend request!"] || ![contentAvailable isEqualToString:@"A friend request you sent has been accepted!"] || ![contentAvailable isEqualToString:@"Someone left a review on your profile!"]) {


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

            static int i=1;
            [UIApplication sharedApplication].applicationIconBadgeNumber = i++;


            NSLog(@"content-available is equal to 1");


            completionHandler(UIBackgroundFetchResultNewData);


        }
}

MapViewController.m

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

    self.notifyCircle.hidden = NO;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"bellAlert" object:nil];

     NSLog(@"BELL ALERT TRIGGERED");


}

MyMessagesTableViewController.m

-(void)viewDidLoad {

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

}

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

     UIImage *image = [UIImage imageNamed:@"circlenotify2.png"];
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     button.frame = CGRectMake(0,0,image.size.width, image.size.height);
     [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
     [button setBackgroundImage:image forState:UIControlStateNormal];

     // BarButton Item
     UIBarButtonItem *navLeftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = navLeftButton;
     self.navigationItem.rightBarButtonItem.badgeValue = @"1";


    NSLog(@"Red Circle Triggered");
}
...