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

4
我的 Firebasemessagingservice 类代码用于传递意图:
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()方法中,我的Fragment BaseActivity代码如下:
Intent notifyIntent = getIntent();
    Bundle extras = notifyIntent.getExtras();
    if (extras != null) {

       replacefragment code;

    }

它不能工作。


请发布替换片段代码,并编辑问题以正确说明您想要实现什么。 - Amrut Bidri
我尝试了NewIntent()方法,但它没有工作。 - Shweta Chauhan
你有检查 NewIntent() 是否被调用了吗? - Piyush
是的,它不叫这个名字。 - Shweta Chauhan
为什么要点踩? - Shweta Chauhan
显示剩余7条评论
2个回答

6

首先,在代码中声明通知时,请按以下方式声明意图:

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

mBuilder.setContentIntent(pendingIntent)非常重要,可以使活动在onNewIntent上运行。 - saintjab

5
您需要发送带有意图数据的密钥,并使用该密钥进行检查和执行代码,如下所示:
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());
}

并检查活动(Activity)的状态,例如:

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

       replacefragment code;

    }

同时检查onIntent变化,以防活动已经打开


你使用哪个 Event 来检查 Activity 上的 getIntent() - Coder Absolute

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接