mitter.io
  • Welcome
  • Migrating from cloud to on-prem
  • Get mitter.io
    • Custom configuration
  • Getting started
    • Build Your First Android App
      • Setup
      • Authenticate a User
      • Start a Basic Chat
      • Selective Deliveries
      • Custom Payloads
    • Build Your First iOS App
      • Overview
      • Installation
      • Basic Setup
      • Receive Push Messages
      • Storyboard
      • Channel List
      • Channel Window
    • Build Your First Web App
      • Setting Up Your App
      • Start a Basic Chat
      • Selective Deliveries
    • Build Your First React Native app
  • Platform Reference
    • Introduction
    • Concepts
    • Authorization and Access
    • Calling the APIs
    • Users
    • Channels
      • Channel Streams and Typing Indicators
    • Messages
    • Delivery Endpoints (Push Notifications)
    • Federated Authentication
    • Basic Permissions and Privileges
    • ACLs and Advanced Permission Model
    • Metadata
  • SDKs
    • Android
      • Getting Started
      • Set up FCM
      • Presence and Timeline Events
      • Profiles, Pagination and Locators
      • Using the UI Framework
    • iOS
      • Installation
      • Basic Setup
      • Get the current user details
      • Create a Channel
      • Messaging
      • Push Messages
    • Javascript
      • Using the UI framework (web only)
      • For react-native
      • For node.js
      • TSDocs / JSDocs
      • For Typescript Users
    • Java (Backend)
Powered by GitBook
On this page
  • Create users from the dashboard
  • Add a user to your app
  • Fetch currently logged-in user details
  1. Getting started
  2. Build Your First Android App

Authenticate a User

Add some users to your app and authenticate via the SDK.

Before we can authenticate a user from our app, we need to create one. You can create a user in either of these 2 ways:

  • Use the dev panel (the "Users" tab when you select an application) in the Mitter.io Dashboard

  • Make an API call from your backend server

In this example, we’ll be creating a user from the Mitter.io Dashboard using the dev panel.

Create users from the dashboard

Head over to the Mitter.io Dashboard and do the following:

  • Select your application from the list

  • In the left sidebar, click on the Users icon, and then click on New User

  • Give a name for your user and create one

Since we’ll be creating a group chat channel in the upcoming steps, go ahead and create 2 or 3 more users with different names.

Now, select one of the users you’ve created and click on Add Token. This will create an access token for the user. Copy the access token and keep it handy.

Follow the same process for creating a channel. Just choose the channel type as Group Chat and add all the users that you created in the previous step. Once your channel is created, you’ll see a channel ID. Copy it and keep it handy as well.

Add a user to your app

Go back to your Android project and navigate to your custom Application class.

First, you need to define a UserAuth object with the user details you acquired in the previous step.

MyApp.kt
val userAuth = UserAuth(
    userId = "8ed92f3c-0696-4513-a842-085e3cee589e",
    userAuthToken = "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJtaXR0ZXItaW8iLCJ1c2VyVG9rZW5JZCI6ImJtRlI5bWNQaDhkQnJHaWIiLCJ1c2VydG9rZW4iOiJiM2dvY2ZhZ3ZyNWlrNHJkbXJlc29wNnNlcyJ9.dlE1QOYmUJpqoh1kORm3hEI3KbBM0v8kKZQnQQwXR6TZuFiCaDQrJMlp-2dgNP1CTYCPMFYoqGctRWQ5JyNiOQ"
)
MyApp.java
UserAuth userAuth = new UserAuth(
    "8ed92f3c-0696-4513-a842-085e3cee589e",
    "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJtaXR0ZXItaW8iLCJ1c2VyVG9rZW5JZCI6ImJtRlI5bWNQaDhkQnJHaWIiLCJ1c2VydG9rZW4iOiJiM2dvY2ZhZ3ZyNWlrNHJkbXJlc29wNnNlcyJ9.dlE1QOYmUJpqoh1kORm3hEI3KbBM0v8kKZQnQQwXR6TZuFiCaDQrJMlp-2dgNP1CTYCPMFYoqGctRWQ5JyNiOQ"
);

You can get the user ID from the list of users in the Users tab. Make sure that the ID and auth token both belong to the same user.

Now, just pass this UserAuth object to your previously configured Mitter object inside the onCreate() method in your MyApp class, like this:

MyApp.kt
mitter = Mitter(
    context = this,
    mitterConfig = mitterConfig,
    userAuth = userAuth
)
MyApp.java
mitter = new Mitter(
    this,
    mitterConfig,
    userAuth
);

Your Android app is now fully configured to make calls to the Mitter.io Platform. Let’s test that out by fetching some basic information for the currently logged-in user.

Fetch currently logged-in user details

Head over to your MainActivity and get a reference to the Mitter object inside the onCreate() method.

MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var mitter: Mitter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mitter = (application as MyApp).mitter
    }
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
    private Mitter mitter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mitter = ((MyApp) getApplication()).mitter;
    }
}

Now, before making any calls, you need to get access to the basic group objects:

  • Users

  • Channels

  • Messaging

Inside your onCreate(), define 3 objects like this:

MainActivity.kt
val users = mitter.Users()
val channels = mitter.Channels()
val messaging = mitter.Messaging()
MainActivity.java
Mitter.Users users = mitter.new Users();
Mitter.Channels channels = mitter.new Channels();
Mitter.Messaging messaging = mitter.new Messaging();

You’re now all set to make calls. Let’s test out our setup by making a call to fetch the currently logged-in user details.

You need to call the getCurrentUser() method on the Users object that you defined just now.

MainActivity.kt
users.getCurrentUser(
    onValueAvailableCallback = object : Mitter.OnValueAvailableCallback<User> {
        override fun onError(apiError: ApiError) {
            Log.d("MSA", "Error while fetching user: $apiError")
        }

        override fun onValueAvailable(value: User) {
            Log.d("MSA", "User is: ${value.screenName.screenName}")
        }
    }
)
MainActivity.java
users.getCurrentUser(new Mitter.OnValueAvailableCallback<User>() {
    @Override
    public void onValueAvailable(User user) {
        Log.d("MSA", "User is: "+ user.getScreenName().getScreenName());
    }

    @Override
    public void onError(ApiError apiError) {
        Log.d("MSA", "Error while fetching user: " + apiError);
    }
});

If you’ve set everything up correctly, you should see something similar to this in your app’s logs:

D/MSA: User is: Jason
PreviousSetupNextStart a Basic Chat

Last updated 6 years ago