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
0.1 Add Firebase Config file

Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to get SHA-1 Key in this example.

0.2 Add dependencies

build.gradle (project-level):

build.gradle (App-level):

0.3 Android Manifest

For Firebase Service:

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 an callback after about 10 messages.

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 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x

3. Project Structure

firebase-cloud-messaging-upstream-messages-structure

4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.fcm.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to have SHA-1 Key in this example, just leave it blank.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.fcm.

4.3 Activity for sending Upstream Messages

4.4 Create class for handling callbacks

Inside com.javasampleapproach.fcm package, create MyFCMClass:

4.5 Add Firebase Service to AndroidManifest

4.6 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:
firebase-cloud-messaging-upstream-messages-ui

– Logcat in Android Studio:

III. Source code

FCM-upstream-messages


Related Posts



Got Something To Say:

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

*