0

I have an android application. In that i have a using Firebase for push notifications. I need to show a popup message in my home page when receive a message in FirebaseMessagingService class

FirebaseMessagingService

String actiondata = remoteMessage.getData().get("action");
if(actiondata.equals("booking")){
        Intent myIntent = new Intent("com.driver.approverequest");
        myIntent.putExtra("contentdata",contentdata);
        myIntent.putExtra("requestid",productId);
        this.sendBroadcast(myIntent);
        Log.d(TAG, "Message data : athira send " + message);
    }

HomeActivity

 public BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getStringExtra("action");
        try {
            JSONObject mJsonObject = new JSONObject(intent.getExtras().getString("contentdata"));
            Log.d("hi...........",intent.getExtras().getString("contentdata"));
            Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_LONG).show();
            StartTrackingDriver();
        } catch (Exception e){

        }
    }
};

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));
}

@Override
protected void onPause() {
    super.onPause();
    try {
        unregisterReceiver(myReceiver);
    }catch (Exception e){}
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Arya
  • 1,729
  • 3
  • 17
  • 35
  • You need to send only data type notification to get call in your onMessageReceived. Check this answer https://stackoverflow.com/a/54787937/5335382 And then fire a broadcast if home activity is already open else start activity. – Shivam Yadav Mar 06 '19 at 06:28

3 Answers3

0

register broadcast in OnCreate()

registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));

unregister in onDestroy()

unregisterReceiver(myReceiver);

unregister in onPause will unregister the broadcast whenever your activity goes in background.

AND

you're getting string "action" from intent but never set the value to intent.

String action = intent.getStringExtra("action");

So set the string to intent first.

myIntent.putExtra("action",actiondata);

EDIT try using LocalBroadcastManager.getInstance(mContext) before register, unregister & sendbroadcast()

LocalBroadcastManager.getInstance(context).registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));

LocalBroadcastManager.getInstance(context).unregisterReceiver(myReceiver);

LocalBroadcastManager.getInstance(context).sendBroadcast(myIntent);
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
0

@Arya Fcm worked on following concept.

1 App is in front 2 App is in background

1 When app is in front then onReceive method will be called each time irrespective with your json payload that you received from server

2 When your app is in background then onReceive method will be called only when your json does not contain "notification" key. If your payload contain notification key then firebase sdk will manage all.

Jaykishan Sewak
  • 822
  • 6
  • 13
0
String actiondata = remoteMessage.getData().get("action");
 if(actiondata.equals("booking")){
     Intent myIntent = new Intent("com.driver.approverequest");
     myIntent.putExtra("contentdata",contentdata);
     myIntent.putExtra("requestid",productId);
     LocalBroadcastManager.getInstance(this).sendBroadcast(myIntent);
     Log.d(TAG, "Message data : athira send " + message);
 }

//In your HomeActivity
@Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
  BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
  @Override
     public void onReceive(Context context, Intent intent) {
         String data = intent.getStringExtra("contentdata");
         String id = intent.getStringExtra("requestid");

 //Write your pop up message logic here
  }
     }

 @Override
 protected void onResume() {
 LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,new IntentFilter("com.driver.approverequest"));
        super.onResume();
 }

 @Override
 protected void onPause() {  
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
     super.onPause();
 }
user3678528
  • 1,741
  • 2
  • 18
  • 24