How to open Fragment on click of push notification

2019-09-07 14:51发布

问题:

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

回答1:

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



回答2:

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