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.
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.
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.ktval userAuth = UserAuth(userId = "8ed92f3c-0696-4513-a842-085e3cee589e",userAuthToken = "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJtaXR0ZXItaW8iLCJ1c2VyVG9rZW5JZCI6ImJtRlI5bWNQaDhkQnJHaWIiLCJ1c2VydG9rZW4iOiJiM2dvY2ZhZ3ZyNWlrNHJkbXJlc29wNnNlcyJ9.dlE1QOYmUJpqoh1kORm3hEI3KbBM0v8kKZQnQQwXR6TZuFiCaDQrJMlp-2dgNP1CTYCPMFYoqGctRWQ5JyNiOQ")
MyApp.javaUserAuth 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.ktmitter = Mitter(context = this,mitterConfig = mitterConfig,userAuth = userAuth)
MyApp.javamitter = 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.
Head over to your MainActivity
and get a reference to the Mitter
object inside the onCreate()
method.
MainActivity.ktclass MainActivity : AppCompatActivity() {private lateinit var mitter: Mitteroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)mitter = (application as MyApp).mitter}}
MainActivity.javapublic class MainActivity extends AppCompatActivity {private Mitter mitter;@Overrideprotected 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.ktval users = mitter.Users()val channels = mitter.Channels()val messaging = mitter.Messaging()
MainActivity.javaMitter.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.ktusers.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.javausers.getCurrentUser(new Mitter.OnValueAvailableCallback<User>() {@Overridepublic void onValueAvailable(User user) {Log.d("MSA", "User is: "+ user.getScreenName().getScreenName());}@Overridepublic 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