I am trying to experiment with android auto and have developed a small app that generates a notification on button click. I receive the notification on my phone but not on the android auto simulator. I am aware of MessagingStyle being used now instead of UnreadConversations, however I can't find a proper source of code example using it.
showNotification() method in MainActivity.java
private void showNotification(Context context) {
Person user = new Person.Builder()
.setName("Reminder")
.build();
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(user)
.setConversationTitle("Reminder!")
.addMessage("Do you want to continue??", System.currentTimeMillis(), user);
// Yes Action (Reply)
Intent yesIntent = new Intent(context, MessagingService.class);
yesIntent.setAction(MessagingService.ACTION_REPLY);
PendingIntent yesPendingIntent = PendingIntent.getService(
context,
0,
yesIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
);
NotificationCompat.Action yesAction =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_send, "Yes", yesPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.build();
// No Action (Mark as Read)
Intent noIntent = new Intent(context, MessagingService.class);
noIntent.setAction(MessagingService.ACTION_MARK_AS_READ);
PendingIntent noPendingIntent = PendingIntent.getService(
context,
1,
noIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
NotificationCompat.Action noAction =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_close_clear_cancel, "No", noPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Reminder")
.setContentText("Do you want to continue?")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(messagingStyle)
.addAction(yesAction)
.addAction(noAction)
.extend(new NotificationCompat.CarExtender());
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
MessagingService.java
public class MessagingService extends Service {
public static final String ACTION_REPLY = "com.example.REPLY";
public static final String ACTION_MARK_AS_READ = "com.example.MARK_AS_READ";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
private void handleIntent(Intent intent) {
if (intent == null) return;
String action = intent.getAction();
if (ACTION_REPLY.equals(action)) {
handleReplyAction();
} else if (ACTION_MARK_AS_READ.equals(action)) {
handleMarkAsReadAction();
}
stopSelf();
}
private void handleReplyAction() {
// Automatically set the reply to "Yes"
String replyMessage = "Yes";
Log.d("MessagingService", "Auto reply sent: " + replyMessage);
// Send the "Yes" response back to MainActivity
Intent mainActivityIntent = new Intent(this, com.example.notificationapp.MainActivity.class);
mainActivityIntent.putExtra("response", replyMessage);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivityIntent);
// Cancel the notification
cancelNotification();
}
private void handleMarkAsReadAction() {
Log.d("MessagingService", "Notification marked as read");
cancelNotification();
}
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(com.example.notificationapp.MainActivity.NOTIFICATION_ID);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}}
I have implemented the above so far. I want the notification generated here to be sent to android auto simulator (DHU) as well. I would really appreciate some help. If you need any other code snippets please do let me know. Thank you in advance! :)
The below worked for me. I still have an issue with sending replies back to the app but that's another problem.
public void showNotification(Context context) {
Person user = new Person.Builder()
.setName("Reminder")
.setKey("reminder_system")
.setImportant(true)
.build();
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(user)
.setConversationTitle("Reminder!")
.addMessage("Do you want to continue??", System.currentTimeMillis(), user);
Intent replyIntent = new Intent(context, MessagingService.class);
replyIntent.setAction(ACTION_REPLY);
PendingIntent replyPendingIntent = PendingIntent.getService(
context,
0,
replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
);
RemoteInput remoteInput = new RemoteInput.Builder(REMOTE_INPUT_RESULT_KEY).build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
android.R.drawable.ic_menu_send,
"Reply",
replyPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.setShowsUserInterface(false)
.addRemoteInput(remoteInput)
.build();
NotificationCompat.Action markAsReadAction =
new NotificationCompat.Action.Builder(
android.R.drawable.ic_menu_close_clear_cancel,
"Mark As Read",
replyPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.setShowsUserInterface(false)
.build();
Intent noIntent = new Intent(context, MessagingService.class);
noIntent.setAction(ACTION_NO);
PendingIntent noPendingIntent = PendingIntent.getService(
context,
2,
noIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
NotificationCompat.Action noAction =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_close_clear_cancel, "No", noPendingIntent)
.build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, MainActivity.CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(messagingStyle)
.addAction(replyAction)
.addAction(markAsReadAction)
.setShortcutId("reminder_conv");
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());
Log.d("MessagingService", "Notification sent with ID: " + notificationId);
notificationId++;
}