Kotlin Firebase Cloud Messaging – How to Send Upstream Messages | Android

In some Firebase Cloud Messaging tutorials, we have known way to receive Messages. Today we’re gonna look at how to send Upstream Messages from an Android Client App.

Related Post: Firebase Cloud Messaging – XMPP Server example to receive Upstream Messages | Spring Integration

I. Way to do

0. Add Firebase to Android App

Follow these steps.

Open Gradle file to check what happens, we can see that some code has been added:
build.gradle (project-level):

build.gradle (App-level):

1. Get Sender ID

Go to Settings of your Firebase Project in Firebase Console to get Sender ID.

2. Send an Upstream Message

Using FirebaseMessaging.send() method:

3. Handle Callbacks

We can implement onMessageSent() and onSendError() to check the status of Upstream Messages in the class that extends FirebaseMessagingService:

onSendError(): returns a SendException with an error code:
.ERROR_INVALID_PARAMETERS: Message was sent with invalid parameters.
.ERROR_SIZE: Message exceeded the maximum payload size.
.ERROR_TOO_MANY_MESSAGES: App has too many pending messages so this one was dropped.
.ERROR_TTL_EXCEEDED: Message time to live (TTL) was exceeded before the message could be sent.
.ERROR_UNKNOWN: Unknown error.

To optimize network usage, these acknowledgement may not be immediate for each message. You may receive callback after about 10 messages.

4. Add Service to Android Manifest

For Firebase Service:

II. Practice

1. Goal

We will build an Android App that can:
– send Message Data from input Text upstream to Firebase when clicking on the Button.
– handle onMessageSent() and onSendError() Callbacks.


2. Technology

– Gradle 3.0.1
– Android Studio 3.x
– Firebase Android SDK 11.x

3. Project Structure


4. Step by step
4.1 Create Android Project & Add Firebase Cloud Messaging

– Generate new Android Project with package com.javasampleapproach.kotlin.firebase.fcm.
– Follow these steps to add Firebase to Android Project.

4.2 Activity for sending Upstream Messages

4.3 Create class for handling callbacks

Inside com.javasampleapproach.kotlin.firebase.fcm package, create MyFCMClass:

4.4 Add Firebase Service to AndroidManifest

4.5 Run & Check result

– Use Android Studio, build and Run your Android App.
– Fill Edit Text in the App UI, then click on UPSTREAM MESSAGE about 10 times:

– Logcat in Android Studio:

III. Source code


By grokonez | December 13, 2017.

Last updated on July 13, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *