The key is to register a custom category identifier for CarPlay, make sure you include .allowInCarPlay
in the category options, and .carPlay
to the list of options when requesting authorization:
enum CategoryIdentifier: String {
case carPlay
}
let category = UNNotificationCategory(identifier: CategoryIdentifier.carPlay.rawValue,
actions: [],
intentIdentifiers: [],
options: [.allowInCarPlay])
notificationCenter.setNotificationCategories([category])
notificationCenter.requestAuthorization(options: [.alert, .sound, .badge, .carPlay]) { (success, error) in
}
and then use the same category when scheduling a notification:
let content = UNMutableNotificationContent()
content.title = "Hello!"
content.subtitle = "This is a test notification."
content.categoryIdentifier = CategoryIdentifier.carPlay.rawValue
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString,
content: content,
trigger: trigger)
notificationCenter.add(request) { (error) in
}