7
votes

I'm using FCM(Firebase Cloud Messaging) for sending push notifications in iOS.

I'm able to receive the notification when App is in foreground state. But when the App is in background state, the notification is not received. Whenever the application will come to foreground state, only then will the notification be received.

My code is:

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

// Pring full message.
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 );
}}



- (void)applicationDidEnterBackground:(UIApplication *)application {


}

When App is in background state:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

-- is not called at all.

I enabled push notifications and also remote notifications in background modes in App Capabilities. But App is still not receiving the notification.

I referred to some StackOverflow questions but wasn't able to solve the issue. Is there anything to add in iOS version 10 or any mistake in my code?

2
Are you only not receiving this for iOS 10 or for all other versions when in background?AL.
@AL. only not receiving for iOS 10Himanth
Send me your coding I will solve the problem.user3182143
@user3182143 In ios 10 we need to add new delegates to handle push notifications. My question is what happens if i already have an app in the appstore. push notifications wont work right?Ganesh Kumar
Yes.For iOS 10 it does not work.If they have iOS 10 on iPhone it won't work.You need to write push coding according to iOS 10 also.user3182143

2 Answers

10
votes

For iOS 10, we need to call the 2 methods below.

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);
    completionHandler(UNNotificationPresentationOptionAlert);
} 

For BACKGROUND state

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

Before that, we must add the UserNotifications framework and import in the AppDelegate.h file

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

We've run many tests with our customers and did lots of research on the topic. iOS 10.x, especially 10.3.x has issues handling and displaying pushes. The only way to circumvent the issues is by upgrading to a newer iOS-version.