简体   繁体   中英

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..

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

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();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM