I'm working with notifications on android
.
The notification appears when it is supposed to and makes a certain question to the user, who in turn only has to answer "yes" or "no" through the two buttons that the same notification makes available.
One of the issues that I am debating is that when the user clicks on one of the buttons, the notification registers what is supposed to (the user response), but the notification remains in the notification bar.
What I want is that, shortly after registering the user action, the notification disappears from the notification bar.
I specify the date and time of the notification.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, data % 100);
calendar.set(Calendar.MONTH, ((data / 100) % 100) - 1);
calendar.set(Calendar.YEAR, data / 10000);
calendar.set(Calendar.HOUR_OF_DAY, horaFim);
calendar.set(Calendar.MINUTE, minutoFim);
calendar.set(Calendar.SECOND, 00);
// Obtém um alarm manager
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
// O id a ser usado no pending intent
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, CreateNotificationReceiver.class);
// Obtém o pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
I create the notification with its buttons
public class CreateNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
int id = extras.getInt("id");
Intent intentSIM = new Intent(context, SIMService.class);
intentSIM.putExtra("id", String.valueOf(id));
PendingIntent resultPendingSIM =
PendingIntent.getService(context,
0,
intentSIM,
PendingIntent.FLAG_CANCEL_CURRENT
);
Intent intentNAO = new Intent(context, NAOService.class);
intentNAO.putExtra("id", String.valueOf(id));
PendingIntent resultPendingNAO =
PendingIntent.getService(context,
0,
intentNAO,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("Responde:")
.setContentText("Queres alomoçar?")
.setContentIntent(resultPendingSIM)
.setAutoCancel(true);
mBuilder.setSmallIcon(R.drawable.outros);
mBuilder.addAction(R.drawable.outros, "Sim", resultPendingSIM);
mBuilder.addAction(R.drawable.outros, "Não", resultPendingNAO);
mBuilder.setPriority(Notification.PRIORITY_MAX);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
mNotificationManager.notify(id, mBuilder.build());
}
}
public class SIMService extends IntentService {
public TPCCriadoService() {
super("name");
System.out.println("estou no SIM");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
System.out.println("SIM");
}
}
If you can give me some tips I would be very grateful.