Martheli
Martheli

Reputation: 981

UNUserNotificationCenter Completion Was Never Called Swift

I have the following code blocks in my AppDelagate:

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

        if response.actionIdentifier == UNNotificationDismissActionIdentifier {
            print ("Message Closed")
        }
        else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
            print ("App is Open")
        }

        // Else handle any custom actions. . .
    }

func showMessage()

    {
        let notification = UNMutableNotificationContent()
        notification.title = "Test"
        notification.subtitle = "This is a test"
        notification.body = "I need to tell you something, but first read this."
        notification.categoryIdentifier = "TESTAPP"
        notification.sound = UNNotificationSound.default()

        let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.5, repeats: false)

        let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }


func applicationDidEnterBackground(_ application: UIApplication) {

        let center = UNUserNotificationCenter.current()
        center.delegate = self

        self.showMessage()


    }

My notification comes up and when I click on the notification to open the app, the app opens up and the console shows this:

App is Open

2017-07-23 14:48:56.182 Form[3255:198009] Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

So it looks like my print to type out "App is open" it displaying but I am getting the completion handler error right below it.

Upvotes: 2

Views: 6283

Answers (2)

Josh Hamet
Josh Hamet

Reputation: 957

You need to make sure you call the completion handler within the method to let the system know you are done processing the notification.

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

    if response.actionIdentifier == UNNotificationDismissActionIdentifier {
        print ("Message Closed")
    }
    else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
        print ("App is Open")
    }

    // Else handle any custom actions. . .

    // Execute completion handler
    completionHandler()
}

Upvotes: 3

Narek  Simonyan
Narek Simonyan

Reputation: 612

Add completionHandler() under your code in

userNotificationCenter

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

    if response.actionIdentifier == UNNotificationDismissActionIdentifier {
        print ("Message Closed")
    }
    else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
        print ("App is Open")
    }

    // Else handle any custom actions. . .
    completionHandler()
}

Upvotes: 5

Related Questions