如何在单击通知时打开android活动

如何在单击通知时打开android活动,android,Android,当我点击通知时,我想打开一个特定的android活动,但问题是当我点击我的通知时,这会打开我的主要活动,例如,我想打开我的名为notification.class的活动,我使用谷歌的firebase,这是我的代码 public class MyfirebaseMessagingService extends FirebaseMessagingService{ @Override public void onMessageReceived(RemoteMessage remoteMessage)

当我点击通知时,我想打开一个特定的android活动,但问题是当我点击我的通知时,这会打开我的主要活动,例如,我想打开我的名为notification.class的活动,我使用谷歌的firebase,这是我的代码

public class MyfirebaseMessagingService extends FirebaseMessagingService{

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    //final Intent intent = new Intent(this, Notificacion.class);
    //startActivity(intent);
    Intent intent = new Intent(this, Notificacion.class);
    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("Sección 15");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificationBuilder.setSound(defaultSound);
    long[] pattern = new long[]{1000,500,1000};
    notificationBuilder.setVibrate(pattern);
    notificationBuilder.setSmallIcon(R.drawable.logo);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());


}

}

为此行中的活动替换Notification.class:

Intent intent = new Intent(this, Notificacion.class);
应该是这样的:

Intent intent = new Intent(this, MyActivity.class);

您创建并传递给挂起的意图是在单击通知时使用的意图,因此将要打开的活动放入意图中,您还可以向意图中添加额外数据,并通过获取意图getIntent来检索它们。

我实际上已经尝试了您的代码,它显示了我想要的活动。我可以给你几条标准建议:-检查清单是否有所需的活动-尝试使用context.getApplicationContext如果答案正确,请将其标记为正确答案!谢谢在我的通知类中,我有一个与对话框一起使用的代码,在这个对话框中是通知发送的更详细的信息,它类似于一个pupup,我真正寻找的是,通过单击我的通知,我运行我的应用程序并显示一个包含完整通知信息的对话框,这就是为什么我要发送我的类通知。class,我不想打开主类,因此,您应该从对话框中打开活动。在对话框中创建一个新的意图,并在用户准备就绪后启动所需的活动。是的,我知道,但在我的通知类中,我有一个与对话框一起使用的代码,在这个对话框中是通知发送的更详细的信息,它类似于一个pupup,我真正寻找的是通过单击我的通知,我运行我的应用程序,并显示一个包含完整通知信息的对话框,这就是为什么我要发送我的类通知。class,我不想打开主类您应该先打开启动器活动,然后获取意图。如果存在,创建新的意图将旧意图中的所有数据与notification.class一起传递给它作为目标活动谢谢,我将处理该解决方案