Skip to content Skip to sidebar Skip to footer

How To Open A Specific Activity Based On Notification

I am currently working on a reminders app in which the user gets a notification with the name of the reminder and is then redirected to an activity which contains the text of the r

Solution 1:

Just change the PendingIntent using another Activity and/or append extra information on the Intent you are using to create the PendingIntent:

Intent launchIntent = new Intent(this, AnotherActivity.class)
launchIntent.putExtra("myKey", "myValue");
//....
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent , 0);
notification.setLatestEventInfo(this, title, text, contentIntent);

And than, in you Activity's onCreate():

//...getIntent().getStringExtra("myKey")
//do your stuff..

Solution 2:

Just pass the value in the intent . eg.

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
contentIntent.putExtra("phone", value);
contentIntent.putExtra("name", value);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, contentIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

Solution 3:

Question has already been anwered and accepted, but here's another method:

intmId=1;
    PendingIntentpendingIntent= PendingIntent.getActivity(this, 0, newIntent(this, TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
    Notification.Builderbuilder=newNotification.Builder(this)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Title")
            .setContentText("Content")
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true)
            .setNumber(1);
    NotificationManagermanager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(mId, builder.build());

Post a Comment for "How To Open A Specific Activity Based On Notification"