Android P Notifications With Examples

In this tutorial, we’ll be discussing the changes introduced in the Notification System and its UI with Android P. We’ll be demonstrating it with a simple Android Application.

Android P Notifications

Android Pie has introduced a new Messaging Style Notification UI which provides simplified conversations.
Moreover, we can now display images with text in the messaging style as well.

With Android P, the addMessage() function of the MessagingStyle Class has changed. Now instead of passing the String, we pass the Person object which ties that notification and its message to that person.

setData is used to display the image on the message.

setSemanticAction is used to set predefined references to notification actions.

Following are the semantic actions available:

  • SEMANTIC_ACTION_NONE
  • SEMANTIC_ACTION_REPLY
  • SEMANTIC_ACTION_MARK_AS_READ
  • SEMANTIC_ACTION_MARK_AS_UNREAD
  • SEMANTIC_ACTION_DELETE
  • SEMANTIC_ACTION_ARCHIVE
  • SEMANTIC_ACTION_MUTE
  • SEMANTIC_ACTION_UNMUTE
  • SEMANTIC_ACTION_THUMBS_UP
  • SEMANTIC_ACTION_THUMBS_DOWN
  • SEMANTIC_ACTION_CALL

setGroupConversation is used to identify a notification group as a conversation.

In the next section, we’ll implement the different types of Notifications in Android P.

Simple Message Notifications

Inside the addMessage, we pass the message, time(long), and the Person object.
setImportant true indicates that the message would be displayed at the top in the conversation if the notification is collapsed.

Message Notification With Icon

By default, the icon displayed is the first letter of the Person.
We can set a custom icon in place as shown below:

Notification With Image

We can set the image type and uri in the setData method as shown below:

setBot to true indicates that the Person type is a machine.

Message Conversation Notification

The messages are displayed in the order in which the addMessage methods are set.

In the following section, we’ll aggregate the above-learned concepts along with semantic actions.
We’ll be using the AndroidX Packaging System.

Project Structure

android-mvvm-pattern (1)

 

Android P Notification Project

Code

The code for the activity_main.xml layout is given below:

The code for the MainActivity.java class is given below:

When the semantic action button is clicked, we show the intent data in a Toast.

The output of the above application in action is given below:

Android P Notification Output

Android P Notification Output

That brings an end to this tutorial. You can download the project from the link below or view the full source code in our Github Repository below.

By admin

Leave a Reply

%d bloggers like this: