如何在点击推送通知时打开片段

时间:2016-11-24 07:19:54

标签: android android-fragments android-notifications

  

我的Firebasemessagingservice类代码,用于传递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());
}
  

onCreate()方法中的My Fragment BaseActivity代码:

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

       replacefragment code;

    }

它不起作用..

2 个答案:

答案 0 :(得分:3)

您需要发送带有意图数据的密钥并检查该密钥并执行

之类的代码
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());
}

并且像

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

       replacefragment code;

    }
如果活动已经打开,

还会检查内容更改

答案 1 :(得分:0)

首先,在代码中,您以另一种方式使通知声明了意图:

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

将意图分配给mBuilder

mBuilder.setContentIntent(pendingIntent);

然后转到活动主界面并在onCreate方法中添加以下代码:

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

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