1

I'm having a problem to connect my app with Android auto. It is made with Xamarin.Android. I linked the XML into the Android Manifest, but it still doesn't work.

The manifest contains:

<meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" />

The XML contains:

<?xml version="1.0" encoding="UTF-8" ?>
<automotiveApp>
  <uses name="notification"/>
</automotiveApp>

This is how I build notification:

NotificationCompat.Builder notificationBuilder = new 
 NotificationCompat.Builder(ApplicationContext);
            notificationBuilder.SetSmallIcon(Resource.Mipmap.ic_push_icon)
                               .SetContentText(msg)
                               .SetWhen(timestamp)
                               .SetContentTitle(content)
                               .SetContentIntent(readIntent)
                               .Extend(new CarExtender()
                                       .SetUnreadConversation(unReadConversation)
                                       .SetColor(ApplicationContext.GetColor(Resource.Color.purple)))
                               .SetChannelId(Fields.CHANNEL_ID)
                               .AddAction(CreateActionFromRemoteInput(replyIntent,remoteInput));
mNotificationManager.Notify(conversation.Id, notificationBuilder.Build());

Any suggestions? Thanks.

EDIT: I'm working with minSdk 21 and targetSdk 26

EDIT:

The only log I had was:

[Notification] See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

[Notification] Use of stream types is deprecated for operations other than volume control

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
Federico
  • 11
  • 1
  • 4
  • you'd probably need to show some code, or errors... Based on your question it could be absolutely anything. =) – JoeTomks Jul 24 '18 at 16:24
  • Where are you setting up the `UnreadConversation` ? And I would add the manifest to your question. – SushiHangover Jul 25 '18 at 01:53
  • I set UnreadConversation in the service – Federico Jul 25 '18 at 07:16
  • From the Getting Started With Auto (https://developer.android.com/training/auto/start/) docs, you'll need to set the target sdk to at least 21. If you don't see any change with the notifications after confirming that, please add some logs when posting the notification, they will help point the root cause. – salminnella Jul 25 '18 at 17:26
  • ok, you obviously have targetSdk at 26, so this should be fine. Logs that are occurring after posting a notification should still help debug what's happening. – salminnella Jul 25 '18 at 18:19
  • @salminnella hi! I tried on another mac. It works well, but not on mine! Notification are shown normally. Could be desk-head-unit the problem? – Federico Jul 25 '18 at 21:06
  • I don't believe the desktop head unit would prevent them from showing. Did you test with the same mobile device on both machines? You can capture and compare the logs between the 2 computers and see if there are any leads and post them here if you see any errors. – salminnella Jul 27 '18 at 20:10
  • I edited my post with the only log that I had :) – Federico Jul 27 '18 at 20:25

3 Answers3

2

I managed to get android auto working with Xamarin.

What i learned is that setting the SetReadPendingIntent() SetReplyAction() is mandatory.

See my test app on github https://github.com/Verthosa/Xamarin_Android_Auto_Test

Verthosa
  • 1,671
  • 1
  • 15
  • 37
0

I don’t see this in the code snippets you provided, but it looks like the one thing that is missing is adding the message to your unReadConversation.

unReadConversation.addMessage(messageString).setLatestTimestamp(currentTimestamp);

More details in the Sending Messages section of the docs, and most likely related to how it can now handle multiple messages in a conversation.

Here’s a more complete snippet that I have working on a sample app to show messages on the desktop head unit for Auto.

private void sendNotification(int conversationId, String title, String message,
    String participant, long timestamp) {

    // Build a pending Intent for reads
    PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
        conversationId,
        createIntent(conversationId, READ_ACTION),
        PendingIntent.FLAG_UPDATE_CURRENT);

    // Build a Pending Intent for the reply action
    PendingIntent replyPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
        conversationId,
        createIntent(conversationId, REPLY_ACTION),
        PendingIntent.FLAG_UPDATE_CURRENT);

    // Build a RemoteInput for receiving voice input in a Car Notification
    RemoteInput remoteInput = new Builder(EXTRA_VOICE_REPLY)
        .setLabel(getString(R.string.reply_by_voice))
        .build();

    // Build an Android N compatible Remote Input enabled action.
    NotificationCompat.Action actionReplyByRemoteInput = new NotificationCompat.Action.Builder(
        R.drawable.notification_icon, getString(R.string.reply), replyPendingIntent)
        .addRemoteInput(remoteInput)
        .build();

    // Create the UnreadConversation and add the participant name,
    // read and reply intents.
    UnreadConversation.Builder unReadConversation =
        new UnreadConversation.Builder(participant)
            .setLatestTimestamp(timestamp)
            .setReadPendingIntent(readPendingIntent)
            .setReplyAction(replyPendingIntent, remoteInput);

    // Add the message to the unread conversation
    unReadConversation.addMessage(message).setLatestTimestamp(timestamp);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.notification_icon)
        .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.android_contact))
        .setContentText(message)
        .setWhen(timestamp)
        .setContentTitle(title)
        .setContentIntent(readPendingIntent)
        .extend(new CarExtender()
            .setUnreadConversation(unReadConversation.build())
            .setColor(getApplicationContext().getResources()
                .getColor(R.color.default_color_light))).addAction(actionReplyByRemoteInput);


    mNotificationManager.notify(conversationId, notificationBuilder.build());
}

private Intent createIntent(int conversationId, String action) {
    return new Intent()
        .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
        .setAction(action)
        .putExtra(CONVERSATION_ID, conversationId)
        .setPackage(getPackageName());
}
salminnella
  • 744
  • 2
  • 6
  • 17
0

I see this thread is 4 years old. But it's still getting some views so I'll drop an answer here if people do stumble upon it to put them on the right path. It depends on the type of Android Auto app you want to create the process is slightly different. Developing for vanilla Android or for Xamarin is surprisingly similar, so be sure to have a look at Android for cars developer documentation.

But remember the way that Xamarin creates your Android manifest is a bit different to how it is done in vanilla Android. By default Xamarin assigns Guids for your service names. Have a look at Microsoft's docs to gain a better understanding how attributes are used to inject tags into your manifest that link up with your classes. If you look at the obj folder in you Visual studio project you can see what the final Android manifest looks like.

I've only worked with the categories that use the CarAppLibrary. In Xamarin you have to install the Xamarin.AndroidX.Car.App.App nuget package and you'll have all the classes at your disposal that they use in the Google developer docs.

Another thing to remember is if you're implementing any of the Java interfaces in your classes (such as OnClickListeners), is to also inherit from Java.Lang.Object so that it implements most of the abstract members for you.

I've created a step-by-step tutorial on YouTube for Maui, seeing as that is Xamarin's evolution. But the steps are exactly the same for Xamarin.

Hope this helps! Best of luck.

CVStrydom
  • 11
  • 5