I have a BroadcastReceiver() and the onReceive method is called even when the app is suspended/dead. I want the onReceive only to be called when the user opens the notification from the home screen, how can I do that?
MainClass:
 broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            }
    };
    final Intent intent = registerReceiver(broadcastReceiver, intentFilter);
BroadCastReceiver class
 Intent intent;
    if(rootActivity.getPackageName().equalsIgnoreCase("myapp"))
    {
        //your app is open
        intent = new Intent();
        intent.setComponent(rootActivity);
    } else {
        //your app is not open,start it by calling launcher activity
        intent = new Intent(context, MainActivity.class);
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(contentIntent);
Manifest
 android:launchMode="singleTop"