I'm implementing rich push notifications with two action buttons using a notification service extension. however I experienced an issue when tapping on the action button app does not launch.
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
...
let acceptAction = UNNotificationAction(identifier: "accept", title: "accept".localized, options: [.foreground])
let rejectAction = UNNotificationAction(identifier: "reject", title: "reject".localized, options: [.destructive])
let newTripRequestCategory = UNNotificationCategory(identifier: "my_category", actions: [acceptAction, rejectAction], intentIdentifiers: [], options: [])
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([newTripRequestCategory])
bestAttemptContent.categoryIdentifier = "my_category"
}
Even though my actions identifiers are perfectly configured and user response is received in the app delegate and handled inside the userNotification(... didReceive) method like so:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async {
let userInfo = response.notification.request.content.userInfo
print("didReceive: userInfo: \(userInfo)")
guard let tripRequestID = userInfo["tripRequestID"] as? String else {
return
}
guard let id = Int(tripRequestID) else {
return
}
switch response.actionIdentifier {
case "accept":
TripRepo.acceptTripRequest(id: id) { _ in } failed: { _ in }
case "reject":
TripRepo.rejectTrip(id: id, feedback: "rejected from notification pannel") { _ in } failed: { _ in }
default: break
}
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
}
- I added
UNUserNotificationCenter.current().delegate = self
insideapplication(... didFinishLaunchingWithOptions)
- I added
.foreground
in theUNNotificationActionOptions