Buttons (Action) in the notification. How to know which one was clicked?

1

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

The problem is knowing which button the user has loaded.

Another thing I did not intend to do but that happens is that the user is redirected to an activity, and I did not want it.

If he clicks one of the buttons, he is redirected to "MainActivty", and what I intend to do only this is that mainActivity only receives any information about which button the user has uploaded, in order to make the necessary registrations. My intention is just that the user responds to the question without being redirected at all.

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

    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());
        }
    }
    
asked by anonymous 29.06.2017 / 17:06

1 answer

0
  

Another thing I did not intend to do but that happens is that the user is redirected to an activity, and I did not want it.

To do this, the PendingIntent used in the setContentIntent() method should be created like this:

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

To delete the notification when clicked use setAutoCancel(true) .

  

The problem is knowing which button the user uploaded.

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

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

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

(...), to make the necessary records. My only intention is for the user to respond to the question without being redirected to anything.

In this case the treatment must be done in a service (possibly 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
        );
    
29.06.2017 / 18:16