-2

My app consists of a notification with three items like Restore, Re-schedule, Cancel, the user can select any three of them. When the user selects cancel, the notification has to clear. When user selects restore or re-schedule, it has to go that particular class. How to know the background functionality for button click in notification?

Here is the MainActivity look's like

public class MainActivity extends AppCompatActivity {

private String title = "Notification Title";
private String subject = "Notification Subject";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showNotification(getApplicationContext());
}

private void showNotification(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    intent.putExtra("LIST_MAIN_TITLE", title);
    intent.putExtra("LIST_SUB_TITLE", subject);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);
    //  NotificationCompat
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setLights(0xFF0000FF, 100, 3000)
                    .setContentText(subject)
                    .addAction(R.drawable.ic_restore_black_24dp, "Restore", contentIntent)
                    .addAction(R.drawable.ic_schedule_black_24dp, "Re-Schedule", contentIntent)
                    .addAction(R.drawable.ic_cancel_black_24dp, "Cancel", contentIntent);
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}}

Here is the notification look like

enter image description here

Karthik
  • 1,199
  • 2
  • 14
  • 23
  • check this link: http://stackoverflow.com/questions/21925688/adding-button-action-in-custom-notification – prakash Sep 21 '16 at 05:55

1 Answers1

0

Create 3 separate pending intents, each of which goes to the action you want. Don't use the same one for all three buttons.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127