Reputation: 2519
I have an iOS App that's pushing notifications (for example when the user is entering a geographic region). I have also a Watch App that gets these notifications when the iPhone is locked. I have 2 Actions in these notifications ("Call", "Go to"), they are correctly displayed in the Apple Watch notification but when the user touches one of these action the callback
userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
from my UNUserNotificationCenterDelegate is never called!
Here's the code in the iOS App:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Register local notification (alert & sound)
// They are used to notify the user when entering/exiting a monitored region
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in
if let theError = error {
NSLog("\(#function) error when request localNotification \(theError.localizedDescription)")
} else {
if !granted {
NSLog("\(#function) Warning local notification not granted")
}
}
})
var actions = [UNNotificationAction]()
actions.append(UNNotificationAction(identifier: "CallId", title: "Call", options:[] )) // UNNotificationActionOptions.authenticationRequired
actions.append(UNNotificationAction(identifier: "CallId2", title: "Go To", options:[UNNotificationActionOptions.authenticationRequired] ))
let notificationCategory = UNNotificationCategory(identifier: "MonitoringRegionCategory", actions: actions, intentIdentifiers: [], options: [])
let categories: Set = [notificationCategory]
UNUserNotificationCenter.current().setNotificationCategories(categories)
return true
}
the code to send the notification (in the iOS App)
let content = UNMutableNotificationContent()
content.title = "title"
content.subtitle = "subtitle"
var message:String
if isEntering {
message = String(format: NSLocalizedString("POI less than %d meters", comment: ""), Int(poi.poiRegionRadius))
} else {
message = String(format: NSLocalizedString("POI more than %d meters", comment: ""), Int(poi.poiRegionRadius))
}
content.body = message
content.badge = 1
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "MonitoringRegionCategory"
let request = UNNotificationRequest(identifier: AppDelegate.LocalNotificationId.monitoringRegionId, content:content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if let theError = error {
NSLog("\(#function) Error with notification add \(theError.localizedDescription)")
}
})
In the WatchApp to code to register and implement the UNUserNotificationCenterDelegate
class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {
func applicationDidFinishLaunching() {
NSLog("\(#function)")
// Perform any final initialization of your application.
UNUserNotificationCenter.current().delegate = self
}
// MARK: UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
NSLog("\(#function)")
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("\(#function)")
completionHandler(.alert)
}
Any idea why userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) is never called when the user select one of the button from the Notification?
Upvotes: 3
Views: 1086
Reputation: 1286
I had the same problem and I solved it by adding the UNNotificationAction
as a .foreground
category. You've to change following line to fix it:
var actions = [UNNotificationAction]()
actions.append(UNNotificationAction(identifier: "CallId", title: "Call", options:[.foreground]))
actions.append(UNNotificationAction(identifier: "CallId2", title: "Go To", options:[.authenticationRequired]))
.foreground
will launch you app and invoke func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
method. You can handle your actions and redirections in this method for the actionable button. Here is the example:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier.lowercased() {
case "call":
WKInterfaceController.reloadRootControllers(withNames: ["CallInterface"], contexts: nil)
default: break
}
completionHandler()
}
Hope it helps!
Upvotes: 0