displaying local notification in CarPlay app

I have created a CarPlay app that displays local notifications. The notification displays on iPhone but in CarPlay, the notification never displays.

When registering for the notification via requestAuthorization, we are passing .carplay into the options. This results in the "Show in CarPlay" option to show up in the Notification settings for the app. However, even with this option enabled, no notification is ever displayed on the CarPlay screen.

The CarPlay app we have created is the automaker type. Does this app type have additional notification restrictions?

Replies

I was able to get this to work by configuring the following:

You have to setup a notification category with .allowInCarPlay option. Then use the category name in the notification's categoryIdentifier field.

    let category = UNNotificationCategory(identifier: "CarPlay", actions: [], intentIdentifiers: [], options: [.allowInCarPlay])

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

and also .carplay option when sending the notification:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge, .carPlay])

I'm trying to fix one of my apps, which uses Local Notifications. Of course when the phone is plugged into CarPlay, nothing happens. Was hoping to get some leads as to how to get Local Notifications on CarPlay. I've played around with your answer, but it still doesn't do anything (and I haven't added CarPlay)

Do you have to add CarPlay to your app (I assume yes). I'm not an auto maker so I assume I can't add CarPlay for Automaker. Can it be done with CarPlay.communication? It appears that SiriKit needs to be added as well?

Basically, what items did you use to get these Notifications to be shown on the CarPlay screen and then announced. There is so little information out there on this topic. Just everyone saying "It can't be done".

Any help would be greatly appreciated.

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
}

@shortcipher Using the exact code you posted, the notification still shows up on the iPhone simulator, not on the CarPlay simulator.

So for now I'm just working with a test app in the simulator. In the test app I've given it a CarPlay entitlement. To make sure that works, I used CarPlay.parking. With code from other parts of the internet, CarPlayTest shows up on the CarPlay simulator. So the entitlement works fine. I switch to CarPlay.communication (which I think is the correct entitlement for this?) and the icon goes away on the CarPlay simulator (I would think this is correct). The notification always shows up on the iPhone simulator. I have deleted the app and reinstalled. .parking (open and closed on CarPlay simulator) and .communication, the notification shows up on the iPhone.

Is this just a simulator problem, and if I get Apple approval for entitlement it will work, or am I missing something else.