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?