0

I get a notification when the app is in background but not when it is in foreground. I did it exactly like it is said here. What am I doing wrong?

   func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, willPresent notification: UNNotification, withCompletionHandler completionHandler:  @escaping (UNNotificationPresentationOptions) -> Void) {
                
        let userInfo = response.notification.request.content.userInfo

       ...
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let otherVC = sb.instantiateViewController(withIdentifier: "HomeVC") as! UITabBarController
            otherVC.selectedIndex = 2
            UIApplication.shared.keyWindow?.rootViewController = otherVC;
            completionHandler([.banner, .badge, .sound])
    }
submariner
  • 308
  • 1
  • 5
  • 23

1 Answers1

-1

If you're not receiving Firebase push notifications when your iOS app is in the foreground, it's possible that you need to handle foreground notifications manually. Firebase Cloud Messaging (FCM) doesn't show notifications automatically when your app is in the foreground; you need to handle the message reception and presentation yourself.

Here's how you can handle Firebase push notifications when your iOS app is in the foreground:

Implement the FCM delegate methods:

In your AppDelegate.swift file, implement the messaging:didReceiveMessage: method to receive and handle messages when your app is in the foreground. This method will be called whenever a message is received, whether the app is in the foreground or background.

import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        // ...
        return true
    }

    // Handle messages received in the foreground.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        // Handle the message here.
        print("Received data message: \(remoteMessage.appData)")
        // You can implement your own logic to display the notification.
    }

    // ...
}

Custom Notification Handling:

In the messaging:didReceive method, you can implement custom logic to display the notification in your app's user interface when it's in the foreground. This might involve creating a custom alert or updating some UI elements to inform the user about the new message.

Testing:

Make sure that your Firebase Cloud Messaging payload includes the necessary data to display the notification when the app is in the foreground. Ensure that you are sending both notification and data fields in the payload.

Here's an example of a payload that includes both notification and data fields:

{
  "notification": {
    "title": "Notification Title",
    "body": "Notification Body"
  },
  "data": {
    "key1": "value1",
    "key2": "value2"
  }
}

When your app is in the foreground, the messaging:didReceive remoteMessage: method will be called, and you can extract the necessary information from the remoteMessage.appData dictionary to display the notification as desired.

By handling push notifications manually when the app is in the foreground, you have full control over how they are displayed and can integrate them seamlessly into your app's user interface.

  • Hi is there are reason why you removed "didReceive response: UNNotificationResponse" ? I need that. And also 'alert' was deprecated in iOS 14.0. – submariner Jul 22 '22 at 17:05
  • @submariner : for foreground notification, the system delivers that notification directly to your app. When a notification arrives, the system calls the userNotificationCenter(_:willPresent:withCompletionHandler:) method of the UNUserNotificationCenter object’s delegate. Use that method to process the notification and let the system know how you want it to proceed. – vaibhav sharma Jul 22 '22 at 17:31
  • @submariner : hope, you got the point. – vaibhav sharma Jul 22 '22 at 17:33