DEVELOPERS BLOG

How to Add Messaging to your Android App

BBM Enterprise SDK is now BlackBerry Spark Communications Platform. Learn more, and download it for free at BlackBerry.com/Spark.

The BlackBerry Spark Communications Platform makes it easy to integrate a messaging experience into your application. Messaging within Spark allows users to share text, files, pictures, or custom data defined by you. Let’s take a look at how the SimpleChat sample displays a conversation style message list.

Displaying a List of Chat Messages

Spark comes with a collection of utility classes (provided as source code for your convenience) that help you accelerate your development.

The ChatMessageList is one of these utilities. It provides an indexed list of chat messages that are lazily loaded, so that you don’t have to worry about your message history’s performance as the chat grows with thousands of messages.

Using the ChatMessageList coupled with Android’s RecyclerView framework to display the messages within a chat, you can achieve smooth and fluid scrolling in your message list, with older messages lazily loading as you scroll back to view them.

To start, instantiate a ChatMessageList with the id of the chat whose messages you want to display, and an IncrementalListObserver to notify your RecyclerView.Adapter whenever a message is added, removed, or changed.

 

//Create the ChatMessageList
mChatMessageList = new ChatMessageList(mChatId);
//Add our IncrementalListObserver to the ChatMessageList
mChatMessageList.addIncrementalListObserver(mMessageListObserver);

The IncrementalListObserver is added to your ChatMessageList as an observer.

//This observer will notify the adapter when chat messages are added, removed or changed.
private IncrementalListObserver mMessageListObserver = new IncrementalListObserver() {
    @Override
    public void onItemsInserted(int position, int count) {
        mAdapter.notifyItemRangeInserted(position, count);
    }
    @Override
    public void onItemsRemoved(int position, int count) {
        mAdapter.notifyItemRangeRemoved(position, count);
    }
    @Override
    public void onItemsChanged(int position, int count) {
        mAdapter.notifyItemRangeChanged(position, count);
    }
    @Override
    public void onDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }
};

Call start() and stop() on the ChatMessageList to start/stop listening to changes on your messages. This will cause your adapter to only update when the chat is displayed to the user.

@Override
protected void onPause() {
    super.onPause();
    //Stop loading messages from the ChatMessageList
    mChatMessageList.stop();
}
@Override
protected void onResume() {
    super.onResume();
    //Start loading messages from the ChatMessageList
    mChatMessageList.start();
}

Creating Chat Message Views

In this example, we display simple text messages, but Spark also supports files, pictures, contacts, calendar, location, voice notes, or any custom data types you define.

Your RecyclerView.Adapter will create and update the views for the chat messages. Simply update your message view’s text field with the ChatMessage.content. You can also left or right align your outgoing and incoming messages by creating different view types based on the Incoming flag on the ChatMessage object.

//Simple ViewHolder for displaying a text message
private class MessageViewHolder extends RecyclerView.ViewHolder {
    TextView messageText;
    MessageViewHolder(View itemView) {
        super(itemView);
        messageText = (TextView)itemView.findViewById(R.id.message_text);
    }
}
 
//Adapter for displaying chat messages
private RecyclerView.Adapter mAdapter = new RecyclerView.Adapter<MessageViewHolder>() {
    private final int TYPE_INCOMING = 0;
    private final int TYPE_OUTGOING = 1;
    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //The incoming message layout is right justified, outgoing left justified
        int layoutRes = viewType == TYPE_INCOMING ? R.layout.incoming_message_item : R.layout.outgoing_message_item;
        View chatView = LayoutInflater.from(ChatActivity.this).inflate(layoutRes, parent, false);
        return new MessageViewHolder(chatView);
    }
    @Override
    public void onBindViewHolder(MessageViewHolder holder, int position) {
        //Get the message to display
        ChatMessage message = mChatMessageList.get(position);
        if (message.getExists() == Existence.MAYBE) {
            return;
        }
        if (message.hasFlag(ChatMessage.Flags.Incoming)) {
            if (message.state != ChatMessage.State.Read) {
                //Bold unread messages
                holder.messageText.setTypeface(null, Typeface.BOLD);
            else {
                holder.messageText.setTypeface(null, Typeface.NORMAL);
            }
        }
        holder.messageText.setText(message.content);
    }
    @Override
    public int getItemCount() {
        return mChatMessageList.size();
    }
    @Override
    public int getItemViewType(int position) {
        //Use the ChatMessage.Flag to determine if the message is incoming or outgoing and use the correct type
        ChatMessage message = mChatMessageList.get(position);
        return message.hasFlag(ChatMessage.Flags.Incoming) ? TYPE_INCOMING : TYPE_OUTGOING;
    }
};

Next Steps

Adding a messaging experience to your application is made simple with Spark. You can download this example and follow a step by step walkthrough by visiting the BlackBerry Spark Communications Platform developer portal and downloading Spark today.

About BBM Enterprise SDK Team