Initial setup

Before you can start chats with other users you must first set up an Activity that will handle the conversation functionality. This conversation Activity will be the core of your chat app and will be used by other Delivered SDK components covered in other section of the documentation.

Creating a conversation Activity

Start up by creating a new Activity and adding the DeliveredConversationFragment provided by the Delivered SDK. This component is responsible for sending and displaying messages in a conversation:

public class ConversationActivity extends AppCompatActivity {
        
   private DeliveredConversationFragment mConversationFragment;
        
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        mConversationFragment = DeliveredConversationFragment.newInstance(chatId);
        fragmentTransaction.add(R.id.conversation_fragment_container, mConversationFragment);
        fragmentTransaction.commit();
	}
	
	...
	
}

Notice in the code snippet above that the DeliveredConversationFragment.newInstance() takes a parameter called chatId. You can pass an empty string for now, as the reason for this will be explained in the next step.

Next, you must let the Delivered SDK know this will be the Activity used for handling all your conversations. To do this call the DeliveredSdk.setNavigation() method passing the InAppNavigation interface from within your Application class as shown here:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        
        DeliveredSdk.setNavigation(new InAppNavigation() {
            @Override
            public void openChat(String chatId, String chatName) {
                final Intent intent = new Intent(getApplicationContext(), ConversationActivity.class);
                intent.putExtra(EXTRA_CHAT_ID, chatId);
                intent.putExtra(EXTRA_CHAT_NAME, chatName);
                startActivity(intent);
            }
        });
    }
}

Inside the openChat overridden method body you must place an Intent pointing to the conversation Activity you just created, and add the chatId and chatName strings as extras. These two extras are provided to you by the openChat method and will be used in the next and final step.

The last thing to do now is adding the following lines to the conversation Activity you created in the first step:

public class ConversationActivity extends AppCompatActivity {
   ...
            
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    String chatName = "";
	    String chatId = "";
	     
	    if (getIntent().getExtras() != null) {
	        mChatId = getIntent().getExtras().getString(EXTRA_CHAT_ID);
	        chatName = getIntent().getExtras().getString(EXTRA_CHAT_NAME);
	    }
	    
	    // The DeliveredConversationFragment is initialised here ...
	}
	
	...
	
}

First we obtain the chatId and chatName extras from the launching Intent. Then, we can pass the chatId to the DeliveredConversationFragment.newInstance() method. As for the chatName string, you can use it to display the chat name anywhere you want in your conversation Activity.

One all the previous steps are done your conversation Activity code should look like this:

public class ConversationActivity extends AppCompatActivity {
        
   private DeliveredConversationFragment mConversationFragment;
        
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    String chatName = "";
	    String chatId = "";
	     
	    if (getIntent().getExtras() != null) {
	        mChatId = getIntent().getExtras().getString(EXTRA_CHAT_ID);
	        chatName = getIntent().getExtras().getString(EXTRA_CHAT_NAME);
	    }
	    
	    FragmentManager fragmentManager = getSupportFragmentManager();
	    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
	    
	    mConversationFragment = DeliveredConversationFragment.newInstance(chatId);
	    fragmentTransaction.add(R.id.conversation_fragment_container, mConversationFragment);
	    fragmentTransaction.commit();
	}
	
	...
	
}

And that’s it! Once your conversation Activity is ready it should look like this:

conversation_empty.png

The DeliveredConversationFragment provides an input area where you can write messages. Supported input types are: text (including emojis), images from local storage, and a GIFs provided by Giphy. The input area also provides an integarted camera you can use to capture images without leaving the application.

emoji.png image.png camera.png gif.png

Now that the conversation Activity is ready, your initial set up is complete. Head up to the Public group chats or One-to-one chats for instructions on how to create your first chat.