Lesson 187. Notifications. Action buttons. Reply.

Lesson 187. Notifications. Action buttons. Reply.


Android 4.1 has the ability to add buttons to messages.

This is done using the addAction method.

Intent deleteIntent = new Intent(this, MyService.class);
deleteIntent.setAction("ru.startandroid.notifications.action_delete");
PendingIntent deletePendingIntent = PendingIntent.getService(this, 0, deleteIntent, 0);

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .addAction(android.R.drawable.ic_delete, "Delete", deletePendingIntent);

We first create a PendingIntent, which will be called after a button is clicked. Then we pass it to the addAction method, and with it the icon and text for the button.

A button will be displayed when the message is opened.

Pressing the button will not close the message itself. If you need to close it, use cancel in the click handler.

You can add up to three Action buttons. Buttons should not duplicate the action that occurs after clicking on a message.

For the latest versions of Android, for some reason the button icon, just the text, is not displayed.

Reply

Starting with API 24, it was possible to add an input line to the message. This can be handy, for example, in chat applications. The user will be able to reply directly to the message.

Consider an example implementation:

// id
int itemId = ...;

// Intent
Intent intent = new Intent(this, MyService.class);
intent.setAction(ACTION_REPLY);
intent.putExtra(EXTRA_ITEM_ID, itemId);

// PendingIntent
PendingIntent replyPendingIntent =
       PendingIntent.getService(getApplicationContext(),
               itemId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// RemoteInput
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_TEXT_REPLY)
       .setLabel("Type message")
       .build();

// Action
NotificationCompat.Action action =
       new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_send,
               "Reply", replyPendingIntent)
               .addRemoteInput(remoteInput)
               .build();

// Notification builder
NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .addAction(action);

// Notification
Notification notification = builder.build();

// Show notification
NotificationManager notificationManager =
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(itemId, notification);

Let’s parse the code in order.

We have some itemId. This may be, for example, the chat id that received the new message.

We create Intent and PendingIntent. Nothing new here. We will call MyService and pass it an itemId. In PendingIntent we use itemId as requestCode.

Next we create RemoteInput. Here we configure everything regarding the input field that will be displayed in the message. In the Bilder constructor it is necessary to specify the key, which we will use in the future, in order to get the text entered by the user from Bundle. You can pass text to the setLabel method, which will be used as a hint in the input field.

Create an Action button using the Bilder. We pass the standard set there: icon, text and PendingIntent. And in the addRemoteInput method we pass the previously created RemoteInput. This will be the Action button of the Reply button, after which a line of input will appear.

We then use the Action created in the message builder, create the message and display it.

We use itemId in the notify method. Accordingly, knowing the chat id, we can always update or delete the message.

Note that the PendingIntent that we create and use in the Reply button will not be used after clicking on the message, or even after clicking on Reply. It will be used when the user clicks on the submit text button.

In this example, by the way, after clicking on the message, nothing will happen, because in the message builder I did not use the setContentIntent method, not to complicate the example.

run

The message creates an Action button Reply. It opens the input bar.

When you click on the submit button, the system launches MyService, which we specified in PedningIntent, and displays a progress bar. But it will rotate indefinitely until the software is updated or the message is deleted.

Let’s see how in MyService we can get user input and remove progress bar from a message:

if (ACTION_REPLY.equals(intent.getAction())) {

   // Get reply text
   CharSequence replyText = null;
   Bundle results = RemoteInput.getResultsFromIntent(intent);
   if (results != null) {
       replyText = results.getCharSequence(EXTRA_TEXT_REPLY);
   }

   // Get itemId
   int itemId = intent.getIntExtra(EXTRA_ITEM_ID, 0);

   // Perform operations with replyText and itemId
   ...

   // Create new notification
   Notification repliedNotification =
           new NotificationCompat.Builder(getBaseContext())
                   .setSmallIcon(R.mipmap.ic_launcher)
                   .setContentText("Replied")
                   .build();

   // Update notification
   NotificationManager mNotificationManager =
           (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   mNotificationManager.notify(itemId, repliedNotification);

}

With RemoteInput.getResultsFromIntent, we get Bundle from Intent. From this Bundle we can retrieve the text entered by the user in the message. To do this, we use the EXTRA_TEXT_REPLY key (previously used in the RemoteInput builder).

Then we get itemId from Intent.

We now have a chat id and text entered by the user. Can we save it to the database, send it to the server or do something else. It depends on the logic of the application.

Next, we need to understand the message. I will remind that after sending the text, it reflects the progress bar. In this example, we create a simple message with the text Replied and replace it (using the same itemId in the notify method) with the message from which the text was sent.

We try to send the text from the message again

This time we have updated the message in the handler and the progress bar is gone.

What you will do with the message after sending the text is your decision. For example, you can just delete it. Or, if you are displaying the latest chat messages in a message, you can update the expiry message to reflect the new message and re-create the Reply button there.




Discuss in the forum [0 replies]

Leave a Comment