1
I’m working with notifications in android
.
The notification appears when it is supposed and asks a certain question to the user, who in turn only has to answer "yes" or "no", through the two buttons that this same notification makes available.
One of the problems I have is that when you click one of the buttons, the notification records what you are supposed to do (the user reply), but the notification stays in the notification bar.
What I intend is that, right after registering the user’s 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 could give me a few tips I would be very grateful.
Make available the code that resembles the situation you described above, so we can have a starting point. Take the Sopt tour and understand how to ask a question for quick answers.
– Bsalvo
How to ask a good question?
– Marcos Henzel
@I made the code available, thanks for the alert
– Nokas