1

I'm using UNUserNotificationCenter 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 the background state, the notification is not received. Whenever the application will come to foreground state, only then will the notification be received.

For register Remote Notification:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if( !error ){
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            });
        }
        else{
            NSLog( @"Push registration FAILED" );
            NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
            NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
        }
    }];
}
else {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

when the app is in the foreground mode, this method is called:

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

But this method is not working in background mode. I referred to some StackOverflow questions but wasn't able to solve the issue. Is there anything to add in iOS version 11?

Merry
  • 420
  • 4
  • 19

1 Answers1

1

If its a remote notification, this method is called in the app delegate:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Handle notification here!
    }
Kakshil Shah
  • 3,466
  • 1
  • 17
  • 31
  • Make sure Background mode is enabled with remote notification – Abhishek Thapliyal Apr 11 '18 at 09:58
  • I'm using UNUserNotificationCenter delegate method for receive remote notification : `- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler` – Merry Apr 11 '18 at 10:49
  • @Kakshil Shah That delegate would be triggered only when notifications arrived and that indicates there is data to be fetched. https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc – Nyein Ei Ei Tun Apr 12 '18 at 03:45