Dreamer Dreamer - 1 year ago 90
Android Question

Notifications in android studio

I have questions about notifications in android studio I want to build an app with two buttons (add notification and the other one is remove notification) when I press add notification a notification is create with two addActions (one is Read it and the other one is Likeit ) if I press first addAction it take me to ResultActivity.class in my app and if i press the second addAction it take me to another activity called Main2Activity.class in my class when I implement this code the two addAxtions take me to the same activity what is the wrong of my code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

NotificationManager nmanger;
static int id=1;

public void buclick(View view) {
NotificationCompat.Builder nbuild= (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setContentTitle("Danger")
.setContentText("the raining is comming soon")
.setSmallIcon(R.drawable.amule);



Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT );


Intent resultIntent_2 = new Intent(this, Main2Activity.class);
TaskStackBuilder stackBuilder_2 = TaskStackBuilder.create(this);
stackBuilder_2.addParentStack(Main2Activity.class);
stackBuilder_2.addNextIntent(resultIntent_2);
PendingIntent resultPendingIntent_2 =stackBuilder_2.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT );





nbuild.setContentIntent(resultPendingIntent);
nbuild.addAction(R.drawable.computer,"Read it",resultPendingIntent);
nbuild.addAction(R.drawable.trashcan_empty,"Link it",resultPendingIntent_2);



nmanger =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nmanger.notify(id,nbuild.build());
id++;
}

public void removeclick(View view) {

nmanger.cancelAll();
}


enter image description here

Answer Source

As I can see you missed out a line of code right after nbuild.setContentIntent(resultPendingIntent);

You are just setting the first Intent but not the second.