gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:41:47 27 4
gpt4 key购买 nike

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;

}

它不工作..

最佳答案

您需要发送带有 Intent 数据的 key 并使用该 key 进行检查并执行如下代码

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;

}

如果 Activity 已经打开,还检查 onIntent 更改

关于android - 如何在点击推送通知时打开 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40780144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com