简体   繁体   中英

Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS

I'm implementing push notifications in iOS 10. Everything working well.

But I need to hit API when APP received push notification (not only in Active state but also in background/terminated).

For this I'm using NSNotificationCenter for listening notification when App received push notification like this :

   - (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

    NSLog(@"%@", userInfo);

    if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
    {
        NSLog( @"INACTIVE" );
        completionHandler( UNNotificationPresentationOptionAlert );
    }
    else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
    {
        NSLog( @"BACKGROUND" );
        completionHandler( UNNotificationPresentationOptionAlert );
    }
    else
    {
        NSLog( @"FOREGROUND" );
        completionHandler( UNNotificationPresentationOptionAlert );
    }

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

}

And I'm listening this notification in ViewController.m like this

    - (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];
}

 - (void)reloadTable:(NSNotification *)notification
{
// Calling API here
}

This is working fine when the app is running in the foreground. But not in background and terminate states.

Is there any mistake by me or anything else I have to implement?

From iOS 10, we must add UserNotifications framework and delegate

So first we need to do below things in appDelegate.h

#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

For FOREGROUND state

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  
willPresentNotification:(UNNotification *)notification  
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler  
{  
  NSLog( @"Handle push from foreground" );  
  // custom code to handle push while app is in the foreground  
  NSLog(@"%@", notification.request.content.userInfo);
}   

This is for BACKGROUND state .

So here you need to add the notification

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  
 didReceiveNotificationResponse:(UNNotificationResponse *)response  
 withCompletionHandler:(void (^)())completionHandler 
{  
  NSLog( @"Handle push from background or closed" );  
 // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background  
  NSLog(@"%@", response.notification.request.content.userInfo);

  //Adding notification here
  [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
}  

didReciveRemoteNotificationNotCalled in iOS 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM