我正在构建一个应用程序,我正在使用FCM通知。当用户点击通知时,我想将他发送到主要活动上的Fragment1(即片段的名称)。我使用intent.putExtra执行此操作但在主活动时我记录了Bundle extras = getIntent()。getExtras()我有空指针异常。为什么我在putExtra中没有任何东西?
这是myFireBaseMessagingService
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("OnMessage", "Received");
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent=new Intent(this, MainActivity.class);
intent.putExtra("action", "goToFragment1");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentText(remoteMessage.getNotification().getBody())
.setContentTitle("Asp")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
这是MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.dugme1);
btn2=(Button)findViewById(R.id.dugme2);
btn3=(Button)findViewById(R.id.dugme3);
dugme=(Button)findViewById(R.id.subscribe);
dugme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance().subscribeToTopic("developeri");
Log.d("Bravo", "Subscribed to developeri!");
}
});
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
StartFragment startFragment=new StartFragment();
ft.add(R.id.myFragment, startFragment);
ft.commit();
btn1.setOnClickListener(btnOnClickListener);
btn2.setOnClickListener(btnOnClickListener);
btn3.setOnClickListener(btnOnClickListener);
Intent intent=getIntent();
Bundle extras=getIntent().getExtras();
Log.d("Bundle", "Bundle is here");
Log.d("Extras", extras.getString("action"));
if (extras!=null && extras.containsKey("action") && extras.getString("action").contains("goToFragment1")){
getSupportFragmentManager().beginTransaction().replace(R.id.myFragment, new Fragment1()).commit();
Log.d("MainActivity", "IF IS HERE");
}
}
Button.OnClickListener btnOnClickListener= new Button.OnClickListener() {
@Override
public void onClick(View view) {
Fragment newFragment;
if (view==btn1){
newFragment =new Fragment1();
}else if(view==btn2){
newFragment =new Fragment2();
}else if (view==btn3){
newFragment =new Fragment3();
}else{
newFragment=new StartFragment();
}
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
};
}
我的logcat
06-07 09:22:49.598 23913-23913 / com.example.dev3.fragmenti E / AndroidRuntime:致命异常:主要 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.dev3.fragmenti / com.example.dev3.fragmenti.MainActivity}: 显示java.lang.NullPointerException 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 在android.app.ActivityThread.access $ 700(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java) 在android.os.Handler.dispatchMessage(Handler.java) 在android.os.Looper.loop(Looper.java) 在android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException 在 com.example.dev3.fragmenti.MainActivity.onCreate(MainActivity.java:53) 在android.app.Activity.performCreate(Activity.java) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 在android.app.ActivityThread.access $ 700(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java) 在android.os.Handler.dispatchMessage(Handler.java) 在android.os.Looper.loop(Looper.java) 在android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
仅在启动活动时调用“创建”。主要依然存在"活着"在收到您的通知时。因此,您应该让代码处理额外的:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("action")) {
String action = intent.getExtras().getString("action");
// Do what ever is needed
if (action.equalsIgnoreCase("gotToFragment1")) {
getSupportFragmentManager().beginTransaction().replace(R.id.myFragment, new Fragment1()).commit();
}
}
}
将onNewIntent()
方法添加到MainActivity(接收新Intent的活动)。
如果您的活动已经开始,则会调用此方法。
答案 1 :(得分:0)
尝试移动你的
intent.putExtra("action", "goToFragment1");
上面
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);