iosswiftunusernotificationcenter

userNotificationCenter didReceive is not being called when tapping a notification


I have set the following local notification:

    let content = UNMutableNotificationContent()
    content.title = "Some Title"
    content.body = "Some text"
    content.sound = UNNotificationSound.default

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false) 

    let request = UNNotificationRequest(identifier: "OneDay", content: content, trigger: trigger)

    notificationCenter.add(request)

And I have added the following to AppDelegate

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

print("Notification Tapped")

if response.notification.request.identifier == "OneDay" {
    print("OneDay Notification Tapped")
}

completionHandler()
}

notificationCenter has been set as:

let notificationCenter = UNUserNotificationCenter.current()

However, none of the above print statements work. The notification is presented, I tap on it, the app is brought to the foreground but nothing is printed to the console.

Am I missing anything here? I've tested in both the simulator and a device. Same result.

I'm working with XCode 11.5, deploying for 12.0.


Solution

  • I'm not sure what is going as I'd have to see the complete code, so I just made a minimal example for you to understand. It also works perfectly fine in the Simulator.

    Please feel free to let me know if you have any questions!

    First make sure you're asking for the user permission before setting your delegate or scheduling any local notifications, otherwise your notifications will fail silently.

    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (isAuthorized, error) in
        // ...
    }
    

    Once you're done asking authorization (and the status is granted), simply set your delegate:

    UNUserNotificationCenter.current().delegate = self
    

    Create your notification:

    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.subtitle = "Subtitle"
    content.body = "Body"
    content.sound = UNNotificationSound.default
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "OneDay", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) { error in
        guard error == nil else {
            return
        }
        // ...
    }
    

    And your delegate methods will get called as expected:

    extension AppDelegate: UNUserNotificationCenterDelegate {
    
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            // ...
            completionHandler([.alert, .badge, .sound])
        }
    
    
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            if response.notification.request.identifier == "OneDay" {
                // ...
            }
            completionHandler()
        }
    }