mitter.io
Search…
Set up FCM
In the last section, Getting Started, you learnt how to successfully setup the Mitter Android SDK and was able to send a message across a newly created channel. It’s time to step up.

Enabling push messaging

In this section, you’ll see how you can get FCM to work with the SDK to receive new messages in real-time via the Push approach we talked about in the last section.

Integrating FCM with your app

First of all, you need to setup FCM in your Android project. The steps for this setup is beyond the scope of this documentation and since Google has done a pretty good job explaining the same, why don’t you go check out their docs if you haven’t already added FCM to your project.
After you’re through with that, the only thing’s that 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 up Mitter 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 these data, click on New FCM Configuration Property
That’s it, Mitter.io can now get your messages delivered to your users in real-time.

Registering a delivery endpoint

Think of a Delivery Endpoint as an address for your app. By registering your delivery endpoint you’re telling Mitter.io to forward any message that might be of your concern to the device where your app is installed.
Let’s see how you can do that with the SDK. The process is pretty straightforward.
Once you’re done setting up FCM in your project you should have your custom implementation of FirebaseMessagingService as something like MyFirebaseMessagingService or whatever name you chose for your implementation during the setup.
In that class, you need to override a method called onNewToken() and get a reference to the Mitter object from your Application class or wherever you chose to initialise it.
Once you have a reference, it’s just a simple method call. Just add this piece of code in your onNewToken() method:
Kotlin
Java
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!
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!

Processing incoming FCM messages

Since FCM is a general purpose push messaging solution, you need to do a little setup for the SDK to actually make sense of the incoming messages.
There are just two simple steps involved:
  • Check whether the message is from Mitter.io
  • Pass the message to the SDK for processing
In a real-world production app, there’s a high chance that you’ll be using FCM for more than a single service. Therefore, it’s a wise choice to put a check to verify whether the incoming message should be processed by the Mitter SDK or by any other SDKs that you might use.
If the message is from Mitter.io, it needs to be forwarded to the SDK for processing.
The process is pretty simple. You just need to add this code in your onMessageReceived() method in your implementation of FirebaseMessagingService class:
Kotlin
Java
1
if (remoteMessage.data.isNotEmpty()) {
2
val messagingPipelinePayload = mitter.parseFcmMessage(remoteMessage.data)
3
4
if (mitter.isMitterMessage(messagingPipelinePayload)) {
5
mitter.processPushMessage(messagingPipelinePayload)
6
}
7
}
Copied!
1
if (!remoteMessage.getData().isEmpty()) {
2
MessagingPipelinePayload messagingPipelinePayload = mitter.parseFcmMessage(remoteMessage.getData());
3
4
if (mitter.isMitterMessage(messagingPipelinePayload)) {
5
mitter.processPushMessage(messagingPipelinePayload, null);
6
}
7
}
Copied!
Here, we’re initially checking if the message is non-empty by verifying the RemoteMessage object received from FCM.
After that is done, we proceed to parsing the message data and getting a MessagingPipelinePayload object in return. This object is in turn put to test by the isMitterMessage() method.
If it indeed is a valid message, we continue in processing this message by calling the processPushMessage() message and passing the previously obtained MessagingPipelinePayload object to it.
Now, the SDK will process all incoming messages and notify you of any relevant event. Speaking of getting notified, we need some mechanism to actually listen to any incoming events from the SDK.
This is exactly what you’ll be learning in the next section.

Listening to incoming events

All that you need to do to is to register some callbacks with the SDK and your app will always be notified of any relevant events.
To register a callback, you need to open up your Application class or wherever you’ve initialised the Mitter object. Then you need to register a callback on that object like this:
Kotlin
Java
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!
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!
Just add your own callbacks or send out events from the Mitter callbacks to notify various parts of your app about any respective events that they need to handle.

Wrap up

Congratulations! Your app now works in real-time. In the next sections, you’ll be learning how to take full advantage of the SDK to spice up your app with presence updation, message read events and more.
Last modified 3yr ago