In this tutorial, you'll explore a simple, yet feature-rich chat app built for the Android platform with Java. Your app will run on Android Phone, using PubNub as the messaging infrastructure and framework to connect your chat users.
Download the code
You can get the full source code from GitHub. The code for this tutorial is in the
Run the project
To run the application in your local environment, do the following:
Follow the instructions in Using your PubNub keys to set up a
gradle.propertieswith your keys in it.
Launch Android Studio. On the welcome screen, choose Open an existing Android Studio project.
Select the root project folder,
chat-examples-java/, and click Open.
Wait for Gradle to download dependencies and sync the project. This could take several minutes.
Choose Run > Run 'app', and pick the device on which you want to run the app. If necessary, create a new virtual device on which to run the app.
Using your PubNub keys
You can store your keys in a
gradle.properties file. Since you'll often need to commit a project-level
gradle.properties file to a source code repository, you should create a new
gradle.properties file inside the
app/ module, and add it to your project's
To set up a secure mechanism to store and use your keys, do the following:
In a terminal window in your project's root directory, run the following commands. Use your own publish and subscribe keys in these commands, replacing
Inside your app-level
build.gradle, bind your keys from the
gradle.propertiesfile with actual Java code. Add the two
buildConfigFieldlines within the
Don't change anything else here. The variables are initialized from your
Sync nowto start using the keys.
After Gradle builds the project, you'll be able to use the keys from your Java code:
Set up your own project
Now that you've seen how we built our app, why not get started making your own? If you don't have Android Studio installed, you can download it from the Android Developers site.
To create your Android Studio project, do the following:
From the Welcome to Android Studio window, choose Start a new Android Studio Project.
Select the Empty Activity project type.
Name your project, set the package name and location, choose
Javaas the Language and click
To install the PubNub Java SDK using the Gradle dependency manager, do the following:
Open your module-level
build.gradlefile. It's typically located at
dependenciesblock, add a new entry:
Sync nowto download and install the dependencies.
Add more features
Visit the SDK Reference section to add more features to your app like unread message counts, typing indicators, read receipts, reactions, and more.