niks niks - 1 month ago 6
Swift Question

Unable to view action buttion for iOS 10 local notification

I'm currently facing problem while viewing action buttons on time-interval notification on iOS 10. I have created two UNNotificationAction objects 'OK' & 'Cancel' added as notification category.

I'm getting notification on simulator without any action button. Below is my code.

let ok = UNNotificationAction(identifier: "OKIdentifier",
title: "OK", options: [])

let cancel = UNNotificationAction(identifier: "CancelIdentifier",
title: "Cancel",
options: [])

let category = UNNotificationCategory(identifier: "message",
actions: [ok, cancel],
minimalActions: [ok, cancel],
intentIdentifiers: [],
options: [])

UNUserNotificationCenter.current().setNotificationCategories([category!])

let content = UNMutableNotificationContent()
content.title = contentTitle
content.subtitle = contentSubtitle
content.body = contentBody

let model: TimeIntervalNotificationModel = notificationsModel as! TimeIntervalNotificationModel

trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: model.timeInterval!, repeats: notificationsModel.repeats) as UNTimeIntervalNotificationTrigger

let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
//handle here
print("Error: Adding notification failed:\(error?.description)")

self.delegate?.didFailToAddNotification(error: error!)
}
}

Answer

Fixed the problem. Forgot to set category identifier property of Notification content.

content.categoryIdentifier = "message"

Definition:

The identifier of the app-defined category object Specify a category identifier to create an actionable notifications. The identifier must belong to a UNNotificationCategory object that you previously registered with your app. When the notification is delivered to the user, the system adds the actions defined in that category object to the notification interface as appropriate.

Comments