Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Fragment on click of push notification

My Firebasemessagingservice class code for passing intent:

private void showNotification(String message){
    Intent intent=new Intent(this, DrawerActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("data", message);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Registry")
            .setContentText(message)
            .setSmallIcon(R.drawable.com_facebook_button_send_icon_blue)
            .setContentIntent(pendingIntent);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}

My Fragment BaseActivity code in onCreate() method:

Intent notifyIntent = getIntent();
    Bundle extras = notifyIntent.getExtras();
    if (extras != null) {

       replacefragment code;

    }

It's not working..

like image 776
Shweta Chauhan Avatar asked Oct 26 '25 16:10

Shweta Chauhan


2 Answers

First, in the code were you make the notification declarate the intent at the next way:

Intent intent=new Intent(getApplicationContext(), MyClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("menuFragment", "MyFragment");
PendingIntent pendingIntent=PendingIntent.getActivity(MyClass.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

Assign the intent to mBuilder

mBuilder.setContentIntent(pendingIntent);

Then go to the activity main and add this code in the onCreate method:

protected void onCreate(Bundle savedInstanceState) {
    onNewIntent(getIntent());
}

Finaly add this method in MainActivity:

@Override
    protected void onNewIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("menuFragment"))
            {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.detail_fragment_container, MyFragment.newInstance() ).commit();
            }
        }
    }
like image 99
Sergio Velásquez Avatar answered Oct 29 '25 08:10

Sergio Velásquez


You need to send a key with intent data and check with that key and perform code like

private void showNotification(String message){
    Intent intent=new Intent(this, DrawerActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("data", message);
     intent.putExtra("KEY", "YOUR VAL");
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Registry")
            .setContentText(message)
            .setSmallIcon(R.drawable.com_facebook_button_send_icon_blue)
            .setContentIntent(pendingIntent);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}

and chech on Activity like

Intent notifyIntent = getIntent();
   String extras = getIntent().getExtraString("KEY");;
    if (extras != null&&extras.equals("YOUR VAL")) {`enter code here`

       replacefragment code;

    }

also check onIntent change fro if activity is already open

like image 29
Bhushan Avatar answered Oct 29 '25 08:10

Bhushan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!