Notification not triggered when app in foreground - iOS

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.

Upvotes: 1

Views: 2061

Answers (3)

Arpit Javerya
Arpit Javerya

Reputation: 473

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..
            }
        }

Upvotes: 3

Tobias Alt
Tobias Alt

Reputation: 483

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

If you like in foreground you have to implemt it.

Upvotes: 1

vivek bhoraniya
vivek bhoraniya

Reputation: 1535

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

Upvotes: 1

Related Questions