Learn how to add actions & extra information to your messages and build a playful and engaging chat experience.
In the previous chapter, we learnt how to send messages to specific people in a group chat with ACLs.
In this chapter, we’ll see how to take the demo app one step further by adding some custom payloads to our outgoing messages.
Schedule meetings on chat
To demonstrate the power and use of custom payloads, we’ll be letting our users schedule meetings on our app. The basic workflow is as follows:
Pick a date while sending a message
Receivers confirm their availability with a Yes/No action
Add the DatePickerDialog
We’ll use the standard DatePickerDialog to render a calendar dialog on the screen and allow the user to pick a date from the displayed calendar.
Get started by adding a button to the chat screen. For this example, you can use this vector drawable as the calendar button. Just copy this code to your project under the drawable package:
Now, navigate to your activity_main.xml and modify the code to look similar to this:
If you compare this file to your previously created layout, you’ll see that we’ve just added an ImageView showing up the calendar icon that we added in the previous step and adjusted the surrounding views to accommodate this icon.
Lastly, go to your MainActivity and add the listener for this button to open up the DatePickerDialog and listen for inputs from the user.
Define an instance variable in your MainActivity to hold the picked date:
Then, add this block of code inside your onCreate() method:
Note: To use the DatePickerDialog you need to have your minSdkVersion set to 24 or above
Choosing a date for the meeting using the built-in date picker dialog
Send a message with the picked date
Now that we’ve added the functionality to capture a date from the user, we can send it along with our message to be used by our RecyclerView to render messages differently.
Get started by adding a new data class (or a POJO, if using Java) to hold the picked date:
Now, go to MainActivity and inside the sendButton.setOnClickListener {} block (or inside the onClick() method, if using Java) , modify your code to this:
What we did here is:
Wrapped our previous sendTextMessage() method inside an if-else block
Wrapped the picked date string inside a PickedDate object ready to be serialised
Constructed a SentTime timeline event for our Message object
Constructed a Message object with all the previous params and a new MessageDatum list
A MessageDatum is a single unit of your custom payload. It has a unique ID to identify the type of payload you’re sending and also the payload in serialised form.
Now, if you type a message in the input field and tap on the calendar icon you’ll see a date picker pop-up. Select a date and then hit Send.
The app will send out a message with the selected date as its payload.
We’ll make use of this payload in the next section where we’ll show a different chat bubble with this date information.
Show the date message bubbles
To make use of the custom payload that we attached to our message, we need to add some custom layouts for our chat bubbles to accommodate the attached date.
Get started by adding these two layouts to your project:
After you’ve added these, the next step is to modify your ChatRecyclerViewAdapter to something like this:
Here, we did a couple of things:
Added our new date message layouts
Mapped them to their specific message types
Rendered each message based on their message type or rather view type
If you run the app now and send a message with a date, you’ll see the date appearing right below your typed message text. Also, your date messages will appear different for different users:
Just the message text and the date for self-messages
Additional Yes/No buttons for other users’ messages
Go ahead, try this out.
The date message received with Yes/No options
Add action to the chat bubble buttons
The only thing that’s left in this chapter is to add some actions to the buttons that we added in the previous step.
First, create two empty classes called YesAction and NoAction. These are basically events that we’ll be sending out on the button clicks and act on them when we receive them in the MainActivity.
After you’ve done that, navigate to the ChatRecyclerViewAdapter and modify the following block inside your ViewHolder class like this:
Here, we’re basically sending out events using the event bus that we previously added to our app, on each button click.
Now, we need to subscribe to these sent events and act on them in our MainActivity. Navigate to your MainActivity and add the following methods:
What we’re doing here is listening to the incoming YesAction and NoAction events and sending out a plain text message accordingly.
This will act as a shortcut confirmation message action for our users, kind of like what you see in popular apps like Google Allo and Facebook Messenger.
Sending out a confirmation message on choosing an action
And that’s the end of the chapter. You’ve successfully added custom payloads to your messages and responded to them accordingly to update your UI.
pickDateButton?.setOnClickListener {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(
this,
{ _, year, month, day ->
pickedDate = "$day/${month + 1}/$year"
},
year,
month,
day
)
datePickerDialog.show()
}
MainActivity.java
pickDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(
MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
pickedDate = dayOfMonth + "/" + (month + 1) + "/" + year;
}
},
year,
month,
day
);
datePickerDialog.show();
}
});
PickedDate.kt
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
data class PickedDate(
@JsonProperty("date") val date: String
)
PickedDate.java
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class PickedDate {
@JsonProperty("date")
String date;
public PickedDate() {
}
public PickedDate(String date) {
this.date = date;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "PickedDate{" +
"date='" + date + '\'' +
'}';
}
}