mitter.io
Search…
Setup
We’ll get started by setting up the SDK first.

Android

The Mitter.io Android SDK is distributed freely via jCenter and you can add it easily to your project by adding it as a standard dependency.

Creating a new project

Create a new Android app project by following the standard procedure listed below. Alternatively, you can skip the setup part by cloning our starter template and loading it into Android Studio.
To create a project from scratch, do the following:
  • Go to File
  • Click on New, followed by New Project
  • Follow the steps, keep everything set to default or customize to your needs

Adding dependency

Now add the Android SDK to your newly created project by opening up your build.gradle file and pasting the following line in the dependencies block:
build.gradle
1
implementation 'io.mitter.android:core:0.1.7'
Copied!
Once that is added, perform a Gradle sync.

Basic setup

Now that you’ve added the SDK, the next step is to add some initial configuration for the SDK to work.
Note: This documentation contains code snippets for both Kotlin and Java. Switch to the relevant tab while building the demo.
To start working, you need to configure a Mitter object with your application details, which you can access from the Mitter.io Dashboard.
Before you proceed, visit the Mitter.io Dashboard, create an application and grab the application ID.
Next, you need to create a custom implementation of the Application class for your app. You can do so by creating a new class and extending the Application class:
Kotlin
Java
MyApp.kt
1
class MyApp: Application() {}
Copied!
MyApp.java
1
class MyApp extends Application {}
Copied!
Once that’s in place, override the onCreate() method. This is where you’ll be configuring the Mitter object.
Your custom Application class should look something like this:
Kotlin
Java
MyApp.kt
1
class MyApp: Application() {
2
//Defining the mitter object
3
lateinit var mitter: Mitter
4
5
override fun onCreate() {
6
super.onCreate()
7
}
8
}
Copied!
MyApp.java
1
class MyApp extends Application {
2
//Defining the mitter object
3
private Mitter mitter;
4
5
@Override
6
public void onCreate() {
7
super.onCreate();
8
}
9
}
Copied!
Just defining the Mitter object isn’t enough. You need to configure it with the application ID that you retrieved from the Mitter.io Dashboard.
Here's how to do that: Just paste this inside your onCreate() method in the MyApp class:
Kotlin
Java
MyApp.kt
1
val mitterConfig = MitterConfig(
2
applicationId = "your-application-id-here"
3
)
4
5
mitter = Mitter(
6
context = this,
7
mitterConfig = mitterConfig
8
)
Copied!
MyApp.java
1
MitterConfig mitterConfig = new MitterConfig(
2
"your-application-id-here",
3
LoggingLevel.BASIC,
4
null
5
);
6
7
mitter = new Mitter(
8
this,
9
mitterConfig,
10
new UserAuth("", "")
11
);
Copied!

Using the SDK with containerised Mitter.io

If you're using the Mitter.io docker container, then you need to override the default API endpoint in the SDK, as follows:
Kotlin
Java
MyApp.kt
1
val mitterConfig = MitterConfig(
2
applicationId = "your-application-id-here",
3
apiEndpoint = MitterApiEndpoint(
4
"http://localhost:11901",
5
"http://localhost:11901"
6
)
7
)
Copied!
MyApp.java
1
MitterConfig mitterConfig = new MitterConfig(
2
"your-application-id-here",
3
LoggingLevel.BASIC,
4
new MitterApiEndpoint(
5
"http://localhost:11901",
6
"http://localhost:11901"
7
)
8
);
Copied!
That’s all you need to do for the very basic configuration. It’s not ready to connect with the platform just yet, but we’ll get to that part in the later sections.

Setup FCM

Mitter.io works hand-in-hand with Firebase Cloud Messaging (FCM) to deliver messages in real-time to Android devices.
To start receiving messages, you need to setup FCM in your Android app and make a couple of changes in your application in the Mitter.io Dashboard.
First of all, you need to setup FCM in your Android project. The steps for this setup are beyond the scope of this tutorial, and since Google has done a pretty good job explaining the same, you can check out their docs if you haven’t already added FCM to your project.
After you’re through with that, the only thing that's left is to feed your FCM server key in your Mitter.io application inside the Dashboard.
You can do that by following these steps:
  • Open the Mitter.io Dashboard and select your application from the list
  • Go to the Properties tab
  • Click on New Property -> Google -> FCM -> FCM Property
  • You’ll get a modal where you need to fill out your app’s instance ID, which can be easily retrieved from the Google Cloud Console
  • Also, you need to feed in your FCM server key, which can be accessed from your FCM admin panel
  • After you’re done feeding this data, click on New FCM Configuration Property
Once you’ve completed these steps, you need to register a delivery endpoint for Mitter.io to deliver your messages.
In your Android project, create a class called MyFirebaseMessagingService which extends FirebaseMessagingService and override the onNewToken() method.
In the onNewToken() you need to get a reference to your Mitter object which you declared in your custom Application class.
Kotlin
Java
MyFirebaseMessagingService.kt
1
class MyFirebaseMessagingService: FirebaseMessagingService() {
2
override fun onNewToken(token: String?) {
3
val mitter = (application as MyApp).mitter
4
}
5
}
Copied!
MyFirebaseMessagingService.java
1
public class MyFirebaseMessagingServiceJava extends FirebaseMessagingService {
2
@Override
3
public void onNewToken(String token) {
4
Mitter mitter = ((MyApp) getApplication()).mitter;
5
}
6
}
Copied!
Now that you have a reference to the Mitter object, you need to register the token with Mitter.io. Just add the following piece of code in your onNewToken() method:
Kotlin
Java
MyFirebaseMessagingService.kt
1
token?.let {
2
mitter.registerFcmToken(
3
it,
4
object : Mitter.OnValueAvailableCallback<DeliveryEndpoint> {
5
override fun onValueAvailable(value: DeliveryEndpoint) {
6
//Delivery endpoint registered
7
}
8
9
override fun onError(error: ApiError) {
10
//Delivery endpoint failed to register, retry
11
}
12
}
13
)
14
}
Copied!
MyFirebaseMessagingService.java
1
if (token != null) {
2
mitter.registerFcmToken(
3
token,
4
new Mitter.OnValueAvailableCallback<DeliveryEndpoint>() {
5
@Override
6
public void onValueAvailable(DeliveryEndpoint deliveryEndpoint) {
7
//Delivery endpoint registered
8
}
9
10
@Override
11
public void onError(ApiError apiError) {
12
//Delivery endpoint failed to register, retry
13
}
14
}
15
);
16
}
Copied!
Once that is done, the next step is to intercept any incoming push messages and pass them to the SDK for processing.
Get started by overriding the onMessageReceived() method and adding this piece of code inside the method:
Kotlin
Java
MyFirebaseMessagingService.kt
1
if (remoteMessage.data.isNotEmpty()) {
2
val mitter = (application as MyApp).mitter
3
val messagingPipelinePayload = mitter.parseFcmMessage(remoteMessage.data)
4
5
if (mitter.isMitterMessage(messagingPipelinePayload)) {
6
mitter.processPushMessage(messagingPipelinePayload)
7
}
8
}
Copied!
MyFirebaseMessagingService.java
1
if (!remoteMessage.getData().isEmpty()) {
2
Mitter mitter = ((MyApp) getApplication()).mitter;
3
MessagingPipelinePayload messagingPipelinePayload = mitter.parseFcmMessage(remoteMessage.getData());
4
5
if (mitter.isMitterMessage(messagingPipelinePayload)) {
6
mitter.processPushMessage(messagingPipelinePayload, null);
7
}
8
}
Copied!
Okay, the SDK is all set to intercept incoming messages and process them. The only thing that’s left is to register a callback on the Mitter object to get notified when a new message arrives.
Head back to your custom Application class and register a OnPushMessageReceivedCallback on the Mitter object that you had already created.
Kotlin
Java
MyApp.kt
1
mitter.registerOnPushMessageReceivedListener(object : Mitter.OnPushMessageReceivedCallback {
2
override fun onChannelStreamData(
3
channelId: String,
4
streamId: String,
5
streamData: ContextFreeMessage
6
) {
7
//Called when there's some streaming data such as typing indicator
8
}
9
10
override fun onNewChannel(channel: Channel) {
11
//Called when a new channel is created where the user is a participant
12
}
13
14
override fun onNewChannelTimelineEvent(
15
channelId: String,
16
timelineEvent: TimelineEvent
17
) {
18
//Called when there's a new timeline event for a channel
19
}
20
21
override fun onNewMessage(
22
channelId: String,
23
message: Message
24
) {
25
//Called when a new message has arrived for the user
26
}
27
28
override fun onNewMessageTimelineEvent(
29
messageId: String,
30
timelineEvent: TimelineEvent
31
) {
32
//Called when there's a new timeline event for a message
33
}
34
35
override fun onParticipationChangedEvent(
36
channelId: String,
37
participantId: String,
38
newStatus: ParticipationStatus,
39
oldStatus: ParticipationStatus?
40
) {
41
//Called when the user has joined a new channel or has been removed from one
42
}
43
})
Copied!
MyApp.java
1
mitter.registerOnPushMessageReceivedListener(new Mitter.OnPushMessageReceivedCallback() {
2
@Override
3
public void onNewMessage(
4
String channelId,
5
Message message
6
) {
7
//Called when a new message has arrived for the user
8
}
9
10
@Override
11
public void onNewChannel(Channel channel) {
12
//Called when a new channel is created where the user is a participant
13
}
14
15
@Override
16
public void onNewMessageTimelineEvent(
17
String messageId,
18
TimelineEvent timelineEvent
19
) {
20
//Called when there's a new timeline event for a message
21
}
22
23
@Override
24
public void onNewChannelTimelineEvent(
25
String channelId,
26
TimelineEvent timelineEvent
27
) {
28
//Called when there's a new timeline event for a channel
29
}
30
31
@Override
32
public void onParticipationChangedEvent(
33
String channelId, String participantId,
34
ParticipationStatus participationStatus,
35
ParticipationStatus participationStatus1
36
) {
37
//Called when the user has joined a new channel or has been removed from one
38
}
39
40
@Override
41
public void onChannelStreamData(
42
String channelId,
43
String streamId,
44
ContextFreeMessage contextFreeMessage
45
) {
46
//Called when there's some streaming data such as typing indicator
47
}
48
});
Copied!
You’re now done with the basic setup. Let’s move on to set up a user to start interacting with the platform and send messages.
Last modified 2yr ago