DEV Community

loizenai
loizenai

Posted on

Firebase Cloud Messaging – How to Send Upstream Messages | Android

https://grokonez.com/android/firebase-cloud-messaging-send-upstream-message-firebase-android

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):
    
    buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.1.0'
    }
    }
    
  • build.gradle (App-level):
    
    dependencies {
    // ...
    compile 'com.google.firebase:firebase-core:11.0.2'
    compile 'com.google.firebase:firebase-messaging:11.0.2'
    }

apply plugin: 'com.google.gms.google-services'

0.3 Android Manifest

For Firebase Service:

<application
    ...
   <activity android:name=".MainActivity">
       ...
   </activity>

   <!-- Firebase Service -->
   <service android:name=".MyFCMClass">
       <intent-filter>
           <action android:name="com.google.firebase.MESSAGING_EVENT" />
       </intent-filter>
   </service>
</application>

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:


FirebaseMessaging fm = FirebaseMessaging.getInstance();
fm.send(new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com")
  .setMessageId(MESSAGE_ID)
  .addData("key-1", "value-1")
  .addData("key-2", "value-2")
  .build());

3. Handle Callbacks

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

More at:

https://grokonez.com/android/firebase-cloud-messaging-send-upstream-message-firebase-android

Firebase Cloud Messaging – How to Send Upstream Messages | Android

Top comments (0)