Android Firebase消息传递:来自onMessageReceived的活动不';t打开

Android Firebase消息传递:来自onMessageReceived的活动不';t打开,android,firebase,android-activity,firebase-cloud-messaging,Android,Firebase,Android Activity,Firebase Cloud Messaging,Firebase通知工作得非常完美。我收到了通知,但我有个问题。我想在点击通知后打开一个特定的活动。如果应用程序是打开的,那么它的工作就完美了。但如果应用程序关闭,则主活动将打开。我不知道为什么。谁能帮帮我吗。我必须换什么?谢谢 以下是onMessageReceived的代码: public class MessageService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteM

Firebase通知工作得非常完美。我收到了通知,但我有个问题。我想在点击通知后打开一个特定的活动。如果应用程序是打开的,那么它的工作就完美了。但如果应用程序关闭,则主活动将打开。我不知道为什么。谁能帮帮我吗。我必须换什么?谢谢

以下是onMessageReceived的代码:

public class MessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent intent=new Intent(this,activity2.class); //To this activity it should also go when the app isn't open

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("App");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.icon);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());


}
}

编辑

Welcome活动(启动器活动):

应用程序处于后台时发送的通知消息: 在这种情况下,通知将发送到设备的系统托盘。默认情况下,用户点击通知会打开应用程序启动器

一种方法是添加“click_action”标志,当用户单击通知时,该标志将告诉系统要打开哪个活动。看


另请参见

当应用程序关闭或处于后台时,来自通知的数据将到达目标中的启动器活动。您可以在oncreate()中进行检查如果您的意图有数据,则打开所需的活动,否则simple将打开启动器活动或主活动,无论您的启动器活动是什么。

这意味着我总是随通知发送其他数据?否通知包含标志或指定的数据类型,您可以使用这些标志或数据类型来区分通知。通过检查该通知,您可以执行进一步的实现…您能告诉我代码是什么意思吗?我必须在哪里编写代码?在onMessageReceived中?将此代码放在启动程序活动的oncreate方法中,该方法在应用程序打开时首先打开
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    if (getIntent().hasExtra("type")) {
        Intent intent=new Intent(this, activity2.class);
        intent.putExtra("type",getIntent().getStringExtra("type"));
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        startActivity(new Intent(this, MainActivity.class));
    }
}