Reputation: 199
I am using firebase to receive messages. When message received (onMessageReceived()). I want to check whether the message contains required string (say "hello"). If yes, i want it to be known to another fragment, so that some method in fragment will be called based on message.
For this i want to create event listener when message received. In my fragment, two APIs will be called. on API will be called onCreatedView(). based on message from firebase , second API should be called. The two API calls should be done within fraction of milliseconds.
MyFireBaseMessage.java
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("message here","message here "+ remoteMessage.getData().toString());
String value = remoteMessage.getData().get("msg");
Log.d("value here","value here"+" "+value);
if (value.equalsIgnoreCase("hello"))
{
//implement custom event listener here
}
}
}
Upvotes: 2
Views: 2137
Reputation:
For this use the Local broadcast receiver in your app
example:
in notification screen use the receiver like this @Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
super.onMessageReceived(remoteMessage);
Log.d("message here", "message here " + remoteMessage.getData().toString());
String value = remoteMessage.getData().get("msg");
Log.d("value here", "value here" + " " + value);
if (value.equalsIgnoreCase("hello"))
{
Intent intent = new Intent("custom-listener");
broadcaster.sendBroadcast(intent)
}
}
and in fragment register broadcast receiver in onResume method like this
getActivity().registerReceiver(broadcastReceiver, new IntentFilter("custom-listener"));
also unregister the receiver in onPause nethod
getActivity().unregisterReceiver(broadcastReceiver);
and create broadcast receiver method in fragment where you can call the api , like this
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
};
Upvotes: 0
Reputation: 10012
One way I can think of right now is using LocalBroadcast. to do this
Amend your Firebase service and put this content
public class MyMessagingService extends FirebaseMessagingService {
private LocalBroadcastManager broadcaster;
@Override
public void onCreate() {
broadcaster = LocalBroadcastManager.getInstance(this);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("message here", "message here " + remoteMessage.getData().toString());
String value = remoteMessage.getData().get("msg");
Log.d("value here", "value here" + " " + value);
if (value.equalsIgnoreCase("hello")) {
Intent intent = new Intent("custom-listener");
broadcaster.sendBroadcast(intent)
}
}
}
Make changes in your fragment class
public class BlankFragment extends Fragment {
Context context;
BroadcastReceiver br;
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Override
public void onResume() {
super.onResume();
setup();
}
@Override
public void onDestroyView() {
super.onDestroyView();
LocalBroadcastManager.getInstance(context).unregisterReceiver(mMessageReceiver);
}
private void setup() {
LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
new IntentFilter("custom-listener"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
}
with broadcasts you will be able to listen to the event you want to. The onReceive method inside the Fragment class will get fired when your if condition is true i.e. value.equalsIgnoreCase("hello")
Apologies for the formatting of this answer. having difficulties adjusting it.
Upvotes: 4
Reputation: 9225
Please try this, if hello is key in message format
if (remoteMessage.getData().containsKey("hello")) {
}
Or if hello is as message string, then try this
Map<String, String> params = remoteMessage.getData();
JSONObject fullObject = new JSONObject(params);
String messageString = fullObject.getString("msg");
Hope it works!!
Upvotes: 0