I've had trouble trying to show notifications in Android Auto.
I have followed the configuration guide for adding support for Android Auto1 and the one for notifications in Android Auto 2. But the documentation for the Notification is not clear to me. So I am asking for help if something is wrong with my code.
Currently, the notification is showing on the device, but not on Android auto. I'm testing using the Desktop Head Unit3
Thanks!
fun builder(subject : String, title: String, context: Context) : String {
val ACTION_REPLY = "com.example.native_notification.Notifications.reply"
val ACTION_MARK_AS_READ = "com.example.native_notification.Notifications.mark_as_read"
try {
//Create the NotificationChannel
val channelId = "NOTIFICATION_AUTO";
val name = "Notificaciones android auto";
val descriptionText = "Notify about status of your services";
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(channelId, name, importance)
mChannel.description = descriptionText;
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
val intent = Intent().apply {
putExtra(EXTRA_MESSAGE, "intent working")
}
val pendingIntent = PendingIntent.getActivity(context, 2 ,intent,PendingIntent.FLAG_UPDATE_CURRENT)
val icon: Icon = Icon.createWithResource(context, R.drawable.ic_logo)
val longMessage: Long = 1
var action: Notification.Action =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Notification.Action.Builder(icon,
"Ok,", pendingIntent)
.setSemanticAction(Notification.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build()
} else {
TODO("VERSION.SDK_INT < P")
}
val p = Person.Builder().setName("Gopass").build()
val style: Notification.Style = Notification.MessagingStyle(p)
.setConversationTitle("this is a title")
.addMessage(Notification.MessagingStyle
.Message("this is the text", longMessage, p))
val newMessageNotification = Notification.Builder(context, channelId)
.setContentTitle(title)
.setContentText(subject)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.ic_logo)
.setLargeIcon(icon)
.setStyle(style)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.addAction(action)
.extend(Notification.CarExtender())
.build()
return notificationManager.notify(0, newMessageNotification).toString();
}
catch(ex: Exception) {
return ex.toString()
}
}