Kike Hatake
Kike Hatake

Reputation: 13

How to open an android activity when I click on a notification

I want open an specific android activity when I click on a notification, but the problem is that when I click on my notifiation this open my MainActivity but for example i want open my activity that is called Notificacion.class, I use a firebase of google and this is my code.

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());


}

}

Upvotes: 0

Views: 87

Answers (2)

Daniel Tadros
Daniel Tadros

Reputation: 2357

The intent you create and pass to the pending Intent is the one used when you click the notification so put the Activity you want to open in the intent and you can also add extra data to the intent and retrieve them by getting the intent getIntent()

Upvotes: 0

Jonathan Aste
Jonathan Aste

Reputation: 1774

Replace "Notificacion.class" for your activity in this line:

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

It should looks like this:

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

Upvotes: 1

Related Questions