Reputation: 981
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
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
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