Lesson 188. Notifications. custom messages

Lesson 188. Notifications. custom messages


Android enables us to create a layout for messages ourselves.

Consider a simple example:

layout / notification.xml




   


Height 64dp – standard message height.

We will only show TextView. It is recommended to use @ style / TextAppearance.Compat.Notification. * Styles for your text to be displayed correctly on any version of Android.

The message binder code looks like this:

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.textView, "Custom notification text");
remoteViews.setOnClickPendingIntent(R.id.root, rootPendingIntent);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
       .setSmallIcon(R.mipmap.ic_launcher)
       .setContent(remoteViews);

We create RemoteViews from the layout file.

With the setTextViewText method, we place the text in View c id = R.id.textView.

And with the setOnClickPendingIntent method, we specify PendingIntent, which will be called when you click on View with id = R.id.root. In our example, root is the LinearLayout root. Accordingly, when you click on the message, this PendingIntent will be used to start Activity / Service / BroadcastReceiver.

The builder still needs to specify an icon that will be visible in the notification area. But the setContentTitle and setContentText methods are not required. Instead, we use setContent and pass RemoteViews created there.

As a result, we will see your message

For comparison purposes, a standard message is displayed.

There is another, newer, way to create custom messages – using the DecoratedCustomViewStyle style.

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.textView, "Custom notification text");
remoteViews.setOnClickPendingIntent(R.id.root, rootPendingIntent);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
       .setSmallIcon(R.mipmap.ic_launcher)
       .setCustomContentView(remoteViews)
       .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

The difference with the old method is that we call the setCustomContentView method, not setContent, and use the DecoratedCustomViewStyle style.

result:

Note that in this case, customize not all the message, but its contents. And the rest of the message, such as the icon, time or action buttons, will remain in place.

Using DecoratedCustomViewStyle enables us to customize and extend the message.

example

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.textView, "Custom notification text");
remoteViews.setOnClickPendingIntent(R.id.root, rootPendingIntent);

RemoteViews remoteViewsExtended = new RemoteViews(getPackageName(), R.layout.extended_notification);
remoteViewsExtended.setTextViewText(R.id.textView, "Extended custom notification text");
remoteViewsExtended.setOnClickPendingIntent(R.id.root, rootPendingIntent);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
       .setSmallIcon(R.mipmap.ic_launcher)
       .setCustomContentView(remoteViews)
       .setCustomBigContentView(remoteViewsExtended)
       .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

Here, we customize both the normal message type (setCustomContentView) and the extended message (setCustomBigContentView).

result:

The height of the extended message layout should be no more than 256dp.




Discuss in the forum [0 replies]

Leave a Comment