3. Handle Callbacks

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

1

2

3

4

5

6

7

8

9

10

11

12

publicclassMyFCMClassextendsFirebaseMessagingService{

@Override

publicvoidonMessageSent(StringmsgId){

// ...

}

@Override

publicvoidonSendError(StringmsgId,Exceptione){

// ...

}

}

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.

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.