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

Asked

Viewed 265 times

3

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

The problem is knowing which button the user pressed.

Another thing that I didn’t intend but happen, is that the user is redirected to an Activity, and I didn’t want that.

If he presses one of the buttons he is redirected to "Mainactivty", and what I only want from this action is for mainActivity to receive only any information about which button the user pressed, in order to make the necessary records. My intention is only that the user answer the question without being redirected to anything.

I’ll leave 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 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());
        }
    }

1 answer

2

Another thing that I didn’t intend but happen, is that the user is redirected to an Activity, and I didn’t want that.

For this purpose the Pendingintent used in the method setContentIntent() should be created like this:

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

To delete notification when clicked use setAutoCancel(true).

The problem is knowing which button the user pressed.

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

When using the method addAction() 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 intention is only that the user answer the question without being redirected to anything.

In this case the processing 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
        );
  • thanks, as soon as I can! @ramaral

  • Managed to implement?

  • I am testing the use of intentService that suggested me @ramaral

  • I put what you suggested to me in mainActivity, I took advantage of the Notification_create class, and I extended the Intentservice...It worked, the notification appeared...but I still have the problem, how do I add action the two buttons? I think I misinterpreted what you told me...?

  • You must create an Intentservice for each of the buttons. In it you must put the code that you want to be executed when the respective button is clicked.

  • I created a pending Intent with an Intentservice for each button as suggested. To test if it really worked I entered a log.cat with a message if the user pressed "yes" and nothing happens...the log.cat does not appear @ramaral

  • Declared Intentservice on Androidmanifest.xml?

  • yes declared <service android:name=".Notification.Registocancelaservice" /> <service android:name=".Notification.Registocriadoservice" /> @ramaral

  • I don’t know what else to say. If you correctly implement what is indicated in the answer, it has to work. It can only be something you are doing wrong.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.