0

I'm working on an app that handles push notifications.

For some reason,

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

Works as expected, but

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

is never called.

Anyone ever come across this situation?

All the usual suspects are in place:

UNUserNotificationCenter.current().delegate = self

is set in didFinishLaunchingWithOptions

let center  = UNUserNotificationCenter.current()
        UNUserNotificationCenter.current().delegate = self
        let options: UNAuthorizationOptions = [.alert, .badge, .sound]
        center.requestAuthorization(options:options) { (granted, error) in
            if error == nil{
                DispatchQueue.main.async() {
                    UIApplication.shared.registerForRemoteNotifications()
                    completion(granted)
                }
            }
        }

is returning true (permission granted)

Other items worth mentioning:

  • The Push notifications are sent via SendBird

  • When the app is in the background, everything works as expected.

Buyin Brian
  • 2,781
  • 2
  • 28
  • 48

1 Answers1

1

Times and again, read the documentation.

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

Asks the delegate how to handle a notification that arrived while the app was running in the foreground.

That is, only when your app is running in FORGROUND and you get a notification will the system call this method to ask you whether this notification should still be shown to the user since they're already inside your app. You can call the completionHandler and pass in options like alert and sound so the user will still see the banner and hear the sound. If you only pass back sound, then the user sees no banner and only hears the notification sound.

Nicholas
  • 747
  • 7
  • 23