1

I'm using firebase to implement push notifications in iOS, with objective c. I have the method application:didReceiveRemoteNotification:fetchCompletionHandler, which should be triggered when the app is in background and the user taps the notification and also when the app is in foreground, according to its description. Thing is it only works in background (or when the app is no running). Am I forgetting something?

Thanks for the help.

Jaime Alcántara Arnela
  • 2,062
  • 5
  • 25
  • 56
  • Even though its [duplicate](https://stackoverflow.com/questions/14872088/get-push-notification-while-app-in-foreground-ios?rq=1) question here is a solution https://stackoverflow.com/a/40756206/3632832 – byJeevan Jun 30 '17 at 12:03
  • yes it won't trigger when you are in foreground, you have to handle that on your own like showing it in alert in iOS less than 10 and above 10 you have a custom delegate method to handle that. – Praveen Kumar Jun 30 '17 at 12:14

3 Answers3

3

You can use this below code:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateActive)
            {
                //app is in foreground
                //the push is in your control
                UILocalNotification *localNotification = 
               [[UILocalNotification alloc] init];
               localNotification.userInfo = userInfo;
               localNotification.soundName = 
               UILocalNotificationDefaultSoundName;
               localNotification.alertBody = message;
               localNotification.fireDate = [NSDate date];
                [[UIApplication sharedApplication] 
               scheduleLocalNotification:localNotification];
            }
            else
            {
               //app is in background:
               //iOS is responsible for displaying push alerts, banner etc..
            }
        }
Arpit Javerya
  • 473
  • 3
  • 12
1

For iOS 10 and above below method is called when application is in foreground:

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

So you need to observe this method to handle notification while app is in foreground

vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
1

The standart example for iOS Firebase Notifications is implemented in AppDelegate like completionHandler(UIBackgroundFetchResultNewData);

If you like in foreground you have to implemt it.

Tobias Alt
  • 453
  • 1
  • 3
  • 11