I've created a Notification
to be displayed on Status Bar.
In the notification I've added a Button
, which performs some action.
To call a specific method, on button click I'm using the approach suggested here.
Code:
// Building notification - this code is present inside a Service
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent activityIntent = new Intent(this, HomeActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setContentTitle(fileName.toString()).setContentText("Subject")
.setSmallIcon(R.drawable.ic_app_icon).setContentIntent(pendingIntent).setAutoCancel(false);
// code to add button
Intent openListIntent = new Intent();
openListIntent.putExtra("list_intent", true);
PendingIntent listPendingIntent = PendingIntent.getActivity(this, 0, openListIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_open_list, "List", listPendingIntent);
Notification notification = notificationBuilder.build();
// Code in HomeActivity.java to perform operation on button click
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
boolean openListFlag = intent.getBooleanExtra("list_intent", false);
if (openListFlag) {
// code to perform operation
}
}
My approach is that I've passed an Extra
boolean value, which is set to true, when button in notification is clicked.
But even if click on only the notification (and not on the button), still the action present in onNewIntent() is getting called.
That is, value for openListFlag
is always setted as true.
How should I make sure, that operation is performed only on button click?