2

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?

Community
  • 1
  • 1
reiley
  • 3,759
  • 12
  • 58
  • 114
  • It looks like intent you are passing has key (AppConstants."list_intent") different than you are accessing in HomeActivity. Are they same? – Mehul Shah Jul 02 '14 at 18:16
  • what is the size of ic_open_list icon? is that making any problem? try to click on different parts of notification – Mehul Shah Jul 02 '14 at 18:59

1 Answers1

1

Just use intent action instead of extras, as suggested here: Determine addAction click for Android notifications

Community
  • 1
  • 1
iluu
  • 406
  • 3
  • 13