himanth himanth - 2 months ago 56
Objective-C Question

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?

Answer

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

Comments