java – Buttons(Action) in the notification. How do I know which one was clicked?

Question:

The purpose of the notification is to show a question to the user, and the user has two answer options, "YES" or "NO".

The problem is knowing which button the user has clicked.

One other thing that I didn't intend but that happens is that the user is redirected to an activity, and I didn't want that.

If he clicks on one of the buttons, he is redirected to the "MainActivty", and what I just want from this action is that the mainActivity receives just any information about which button the user clicked, so as to make the necessary records. My intention is just that the user answers the question without being redirected to anything.

Here I leave the piece of code where all this happens. I hope I was as explicit as possible, and I would like to know if what I described above is possible to do.

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ....
                    int id = (int) System.currentTimeMillis();

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.DAY_OF_MONTH, 29);
            calendar.set(Calendar.MONTH, 5);
            calendar.set(Calendar.YEAR, 2017);
            calendar.set(Calendar.HOUR_OF_DAY, 17);
            calendar.set(Calendar.MINUTE, 38);
            calendar.set(Calendar.SECOND, 00);

            AlarmManager alarm = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);

            Intent newintent = new Intent(this, Notification_Create.class);
            intent.putExtra("id", id);
            PendingIntent pending = PendingIntent.getBroadcast(this, id, newintent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending);
        }
    }

    public class Notification_Create extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int id = extras.getInt("id");

            Bundle extras = intent.getExtras();

            Intent intentTPC = new Intent(context, MainActivity.class);
            intentTPC.putExtra("id", String.valueOf(id));
            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(context,
                            0,
                            intentTPC,
                            PendingIntent.FLAG_CANCEL_CURRENT
                    );
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setContentTitle("Organização: ")
                    .setContentText("Tu hoje vais à natação?")
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true);
            mBuilder.setSmallIcon(R.drawable.ic_button);
            mBuilder.addAction(R.drawable.ic_button,"Sim",resultPendingIntent);
            mBuilder.addAction(R.drawable.ic_button,"Não",resultPendingIntent);
            mBuilder.setPriority(Notification.PRIORITY_MAX);

            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(id, mBuilder.build());
        }
    }

Answer:

One other thing that I didn't intend but that happens is that the user is redirected to an activity, and I didn't want that.

For this, the PendingIntent used in the setContentIntent() method must be created like this:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);

To delete the notification on click use setAutoCancel(true) .

The problem is knowing which button the user has pressed.

If you want each button to correspond to a different behavior, create a different PendigIntent for each one:

When using the addAction() method, pass the PendingIntent corresponding to this button(Action).

mBuilder.addAction(R.drawable.ic_button,"Sim",resultPendingIntentSim);
mBuilder.addAction(R.drawable.ic_button,"Não",resultPendingIntentNão);

(…), in order to make the necessary records. My intention is just that the user answers the question without being redirected to anything.

In this case, the treatment must be done in a service (eventually an IntentService).
When creating PendingIntent use getService() instead of getActivity() :

Intent intentSim = new Intent(context, SimService.class);
PendingIntent resultPendingIntentSim =
        PendingIntent.getService(context,
                0,
                intentSim,
                PendingIntent.FLAG_CANCEL_CURRENT
        );
Scroll to Top