Search code examples
javaandroidnotificationsandroid-push-notification

Android: Using Person with NotificationCompat


I want to use Android's Person class to create a sender of a notification so the user image is displayed in MessagingStyle notifications in Android P.

However, the NotificationCompat.MessagingStyle class doesn't support using the Person class, it only seems to be supported by Notification.MessagingStyle (see the addMessage method).

Is there any way to use the Person class with NotificationCompat?

This is what I want to do:

NotificationCompat.MessagingStyle msgStyle = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(notification); 

Person.Builder builder = new Person.Builder().setName(name).setIcon(icon);

msgStyle.addMessage(message, System.currentTimeMillis(), builder.build());

In case there isn't, I need to use the NotificationCompat.MessagingStyle class because I'm using the method "extractMessagingStyleFromNotification" to keep the existing messages in the notification, that way I only need to add the new one. Is there any way to achieve the same using Notification.MessagingStyle?


Solution

  • The solution is to use AndroidX instead of Android Support Library, since the latter is no longer updated.