I’m developing a watchOS app where I need to handle notifications. I’ve implemented the UNUserNotificationCenterDelegate
methods, but it seems that userNotificationCenter(_:didReceive:withCompletionHandler:)
is not being called when a notification is received. while userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification:
is working perfectly.
I’ve checked similar questions but haven’t found a solution that works for me:
userNotificationCenter(_:didReceive:withCompletionHandler:) Contains Empty Notification – watchOSWhy is userNotificationCenter(_:didReceive:) Not Fired?
Here I have attached the minimum reproducible example.
import Foundation
import UserNotifications
class NotificationManager: NSObject, UNUserNotificationCenterDelegate {
static let shared = NotificationManager()
private override init() {
super.init()
}
/// Request notification permissions from the user.
func requestNotificationPermission() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { granted, error in
if let error = error {
print("Error requesting notification permissions: (error)")
} else {
UNUserNotificationCenter.current().delegate = self
print("Notification permissions granted: (granted)")
}
}
}
/// Schedule a test notification to trigger immediately.
func scheduleTestNotification() {
let content = UNMutableNotificationContent()
content.title = "Test Notification"
content.body = "This is a test notification."
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { error in
if let error = error {
print("Error scheduling test notification: (error)")
} else {
print("Test notification scheduled.")
}
}
}
// Handle notification responses
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Notification received in the background: (response.notification.request.identifier)")
completionHandler()
}
// Handle notification delivery while the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification received in the foreground: (notification.request.identifier)")
completionHandler([.sound, ])
}
}
Here is my ContentView
import SwiftUI
struct ContentView: View {
var notificationManager = NotificationManager.shared
var body: some View {
VStack {
Button("Request Notification Permission") {
notificationManager.requestNotificationPermission()
}
.padding()
Button("Schedule Test Notification") {
notificationManager.scheduleTestNotification()
}
.padding()
}
.onAppear {
notificationManager.requestNotificationPermission()
}
}
}
#Preview {
ContentView()
}
The userNotificationCenter(_:didReceive:withCompletionHandler:)
method is not being called when the notification is delivered. I’ve set up everything according to the documentation, but it seems to be failing to trigger.