我的 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;
}
它不工作..
首先,在代码中,您是否让通知声明了以下方式的意图:
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);
然后转到 Activity main 并在 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();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)