I am programming an app that receives some data from a FCM message, then updates the UI based on the data payload contained in the message.
The service runs fine, and receives the data, but I cant figure out how to get it back to my main activity.
The Firebase service is declared in my manifest as
<service android:name=".MyFirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
In my MyFirebaseService class I have overridden the onMessageReceived method, but how am I supposed to notify my main activity that a message was received?
Is there a handler I can use with the Firebase service?
dev.android.com > Develop > Training > Background Jobs > Reporting Work Status
https://developer.android.com/training/run-background-service/report-status.html
To send the status of a work request in an IntentService to other
components, first create an Intent that contains the status in its
extended data.
Next, send the Intent by calling
LocalBroadcastManager.sendBroadcast(). This sends the Intent to any
component in your application that has registered to receive it.
Intent localIntent = new Intent(BROADCAST_ACTION).putExtra(DATA_STATUS, status);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
To receive broadcast Intent objects, use a subclass of
BroadcastReceiver. In the subclass, implement the
BroadcastReceiver.onReceive()
callback method, which
LocalBroadcastManager invokes when it receives an Intent.
LocalBroadcastManager passes the incoming Intent to
BroadcastReceiver.onReceive()
.
To register the BroadcastReceiver and the IntentFilter with the
system, get an instance of LocalBroadcastManager and call its
registerReceiver()
method.
LocalBroadcastManager.getInstance(this)
.registerReceiver(mDownloadStateReceiver, statusIntentFilter);
here is my messaging handle
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("title", fcmTitle);
intent.putExtra("summary", fcmSummary);
intent.putExtra("message", fcmMessage);
intent.putExtra("imageUrl", fcmImageUrl);
intent.putExtra("goto", fcmGoto);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(App.getAppContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
you can have something like this to listen in main activity
Boolean bolGoto = false;
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
//String value = getIntent().getExtras().getString(key);
if (key.equals("goto")){
bolGoto = true;
}
}
}
if (bolGoto){
handleFCMNotification(getIntent());
}