Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification not received when App is in Background in iOS 10

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?

like image 680
Himanth Avatar asked Sep 20 '16 18:09

Himanth


People also ask

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

Why push notification is not working for iOS?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.

Can you send local notifications while app is in background?

Notifications could be created at any moment (on Foreground, Background or even when the application is terminated/killed).

Does Apple block push notifications?

iOS enables you to both disable push notifications entirely, or turn them off for individual apps. To access iOS notifications settings, go into the Settings > Notifications menu. From that list, you can configure each app's notifications settings or disable them.


1 Answers

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>  
like image 72
user3182143 Avatar answered Sep 20 '22 03:09

user3182143