title: Add onClick event to action button in notification android
body: <p>I have gone through a couple of stackoverflow answers and also a couple of tutorials to add actions to the action buttons in notifications in android. I am using the NotificationCompat to create a notification and the notification has two action buttons. Since, the third argument in the action button is a pending intent, I was curious whether there is a way to attach a click event method directly to the action button. Please find the code for the BroadcastReceiver and the place where I create the notification.</p>
  
  <p>AlarmManager:</p>
  
  <pre><code> notifyMed.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
              @Override
              public boolean onMenuItemClick(MenuItem item) {
                  Toast.makeText(mContext, "Notify clicked", Toast.LENGTH_LONG).show();
                  Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
  
                  Intent alertIntent = new Intent(mContext, AlertReceiver.class);
  
                  AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
  
                  alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime ,
                          PendingIntent.getBroadcast(mContext , 1 , alertIntent , PendingIntent.FLAG_UPDATE_CURRENT));
  
                  return false;
              }
          });
  </code></pre>
  
  <p>Here is the code for BroadCastReceiver:</p>
  
  <p>BroadCastReceiver:</p>
  
  <pre><code>public class AlertReceiver extends BroadcastReceiver {
  
  int notificationId = 6324;
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
  
      createNotification(context, "Medication Notification" , "It is time to take your medication", "Message From PHMS");
  }
  
  public void createNotification(Context context, String msgTitle, String msgContentText, String msgTicker){
  
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
              new Intent(context, MedicationNotification.class) , 0);
  
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
              .setSmallIcon(R.drawable.cardiogram)
              .setContentTitle(msgTitle)
              .setTicker(msgTicker)
              .setContentText(msgContentText);
  
      Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      mBuilder.setSound(uri);
  
      long[] vibrate = {500,1000};
      mBuilder.setVibrate(vibrate);
  
  
      mBuilder.addAction(R.drawable.ic_snooze_black_24dp,"Snooze", pendingIntent);
      mBuilder.addAction(R.drawable.ic_check_circle_black_24dp,"Take Medication", pendingIntent);
  
      mBuilder.setContentIntent(pendingIntent);
  
      mBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
  
      mBuilder.setAutoCancel(true);
  
      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  
      mNotificationManager.notify(notificationId, mBuilder.build());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the code for androidManifest.xml</p>
  
  <pre><code>&lt;receiver android:name=".activities.AlertReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;activity android:name=".activities.MedicationNotification"&gt;&lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>I just want to handle a click event on the notification action buttons. The notification is generated correctly. Kindly help!</p>