0

I am using firebase for push notification. I want to show a notification to the user when the application is in the foreground.

Notifications are working fine when the application is closed. But I want to display a stock iOS notification banner even if the application is active.

I have referred below link:

Displaying a stock iOS notification banner when your app is open and in the foreground?

I want to show notification exactly the same way as shown in above link.

I have already implemented these two methods:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        completionHandler(.alert)
    }

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

      completionHandler()
}

Can someone please give me a sample of code for showing push notifications when the application is in the foreground?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Pankaj Mundra
  • 1,401
  • 9
  • 25
  • @Sh_Khan That I know but how can I achieved that? – Pankaj Mundra May 29 '18 at 13:13
  • Tried to send a push when app opened ? , Also your current code doesn't compile tried what in answer ? – Shehata Gamal May 29 '18 at 13:15
  • In foreground, detect the remote notification in didRecieveRemoteNotification and send a local notification – Alwin May 29 '18 at 13:35
  • 2
    Possible duplicate of [Getting local notifications to show while app is in foreground Swift 3](https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3) – Alwin May 29 '18 at 13:47
  • Your question is not related to Firebase – Anton Belousov May 29 '18 at 14:09
  • @AntonBelousov I don't know why you say that, Firebase swizzle the user Notification Center methods to do their own logic so the answer to this question is 100% dependent on the context that firebase is being used! – simonthumper Feb 11 '20 at 10:17

2 Answers2

4

You can try

@available(iOS 10, *)

    extension AppDelegate : UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

          completionHandler([.alert, .sound])

    }

}

For more info check here Notifications

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

You can call this function, for push notification:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
     print("\(userInfo)")
}