This enhancement for notifications is focused around the display of messages within the notification content. The first part that we’re going to look at is the display of the some author when it comes to these messages. To display messages within your notifications you may have previously used the addMessage() function — this function allowed you to pass in a sender name, displaying a textual representation of who the message in the notification has come from. This function is now deprecated and has been replaced with a new addMessage() function — this replacement allows us to pass in an instance of a Person which will be used to tie that message to a specific person and use those details for contextual representation within the notification content. This can allow us to provide more useful content to our notifications, improving the experience here for our user.

Now, when it comes to building a notification, you’ll still it in the same way:

val notificationBuilder = NotificationCompat.Builder(this, ...)

.setSmallIcon(...)

.setContentTitle(...)

.setContentText(...)

.setPriority(...)

We’ll need that reference later, but we’ll leave it as it is for now — let’s go ahead and create an instance of the Person class that we previously mentioned. We’ll use the build of this class to create a simple Person instance that has a name assigned to it:

val sender = Person.Builder()

.setName(R.string.user_name)

.build()

We can then go ahead and use the addMessage() function, passing in our text to be shown in the notification, along with the time and the sender which the message is bound to. Remember, you need to use the setBuilder() function here to reference the notification builder that we previously defined:

NotificationCompat.MessagingStyle(sender)

.addMessage("Check this out!", Date().time, sender)

.setBuilder(notificationBuilder)

Finally, you can go ahead and show your notification just like you previously would have:

NotificationManagerCompat.from(this)

.notify(..., notificationBuilder.build())

When this notification is displayed, you’ll notice that our message is shown, along with the sender name and the default icon for the Person instance:

Whilst this is pretty neat, we can do a little better here! One of the other properties which we can set on a Person is the icon to be used, this is done by using the setIcon() function which takes an instance of an Icon:

val sender = Person.Builder()

.setName(R.string.user_name)

.setIcon(Icon.createWithBitmap(bitmap))

.build()

If we go ahead and show the notification again then you’ll notice that we now have a nice visual representation of the person who is tied to that notification message:

The person class also has a collection of other properties which we can apply using the following functions: