Async- Async- - 3 months ago 86
iOS Question

Cannot invoke initializer for type UNNotificationCategory when migrating to iOS10

I am using an example project to work around new notifications, transition from Swift 2.2 to Swift 2.3, also from iOS 9 to iOS 10. I am using the code below, taken from this example:

@available(iOS 10.0, *)
func setupAndGenerateLocalNotification() {
// Register an Actionable Notification
let highFiveAction = UNNotificationAction(identifier: NotificationActions.HighFive.rawValue, title: "High Five", options: [])

//next line is an error:
let category = UNNotificationCategory(identifier: "wassup", actions: [highFiveAction], minimalActions: [highFiveAction], intentIdentifiers: [], options: [.customDismissAction])
//Cannot invoke initializer for type 'UNNotificationCategory' with an argument list of type '(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)'

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

let highFiveContent = UNMutableNotificationContent()
highFiveContent.title = "Wassup?"
highFiveContent.body = "Can I get a high five?"

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

let highFiveRequestIdentifier = "sampleRequest"
let highFiveRequest = UNNotificationRequest(identifier: highFiveRequestIdentifier, content: highFiveContent, trigger: trigger)
UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(highFiveRequest) { (error) in
// handle the error if needed
print(error)
}
}


The error I get is

Cannot invoke initializer for type 'UNNotificationCategory' with an argument list of type '(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)'

Answer

You didn't mention what your error was, but I suspect the code you copied was written for an earlier beta than you have installed.

I'm using Swift 3 in my work, rather than Swift 2.3. There, as of Xcode 8 beta 5, you should change UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest to UNUserNotificationCenter.current().add, and also remove minimalActions: [highFiveAction], from your UNNotificationCategory. It's possible only the latter might apply in Swift 2.3?

I hope that will get you going, but be warned: this might change again before iOS 10 goes final. In particular, I suspect current() might become current.