Notifications repeated every day

0

I'm trying to make an app that gets notifications at user-determined times every day, but I was only able to get them to arrive once.

This is the code I have so far:

private void salvar() {    
    RespostasAguaCasa p = new RespostasAguaCasa();

    p.setId(1);
    p.setValoragua(QuintaperguntaAguaCasa.valoragua);
    p.setAcordarhora(EscolhaAguaCasa.hourx);
    p.setAcordarminu(EscolhaAguaCasa.minutex);
    p.setDormirhora(SegundaperguntaAguaCasa.hourxx);
    p.setDormirminu(SegundaperguntaAguaCasa.minutexx);
    p.setTerceirahora(TerceiraperguntaAguaCasa.hourxxx);
    p.setTerceiraminu(TerceiraperguntaAguaCasa.minutexxx);
    p.setQuartahora(QuartaperguntaAguaCasa.hourxxxx);
    p.setQuartaminu(QuartaperguntaAguaCasa.minutexxxx);
    p.setQuintahora(QuintaperguntaAguaCasa.hourxxxxx);
    p.setQuintaminu(QuintaperguntaAguaCasa.minutexxxxx);   

    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();
    calSet.setTimeInMillis(System.currentTimeMillis());
    calSet.set(Calendar.HOUR_OF_DAY , EscolhaAguaCasa.hourx);
    calSet.set(Calendar.MINUTE, EscolhaAguaCasa.minutex);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);
    setAlarm(calSet);

}

Alarm:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(arg0)
                .setSmallIcon(R.drawable.casa)
                .setContentTitle("ads")
                .setContentText("sad")
                .setAutoCancel(true)
                ;

        NotificationManager notificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());

    }
}

setAlarm is in the same class as salvar :

public void setAlarm(Calendar targetCall)
{
    Toast.makeText(this, "Alarm is set at" + targetCall.getTime(),
            Toast.LENGTH_LONG).show();
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);

    PendingIntent pendingintent = PendingIntent.getBroadcast(getBaseContext(), 1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCall.getTimeInMillis(), pendingintent);
}
    
asked by anonymous 09.07.2018 / 21:54

1 answer

0

By working the way you are working, the easiest way to do it would be to generate the new notification when the notification is triggered, something like this:

private void salvar() {    
    RespostasAguaCasa p = new RespostasAguaCasa();

    p.setId(1);
    p.setValoragua(QuintaperguntaAguaCasa.valoragua);
    p.setAcordarhora(EscolhaAguaCasa.hourx);
    p.setAcordarminu(EscolhaAguaCasa.minutex);
    p.setDormirhora(SegundaperguntaAguaCasa.hourxx);
    p.setDormirminu(SegundaperguntaAguaCasa.minutexx);
    p.setTerceirahora(TerceiraperguntaAguaCasa.hourxxx);
    p.setTerceiraminu(TerceiraperguntaAguaCasa.minutexxx);
    p.setQuartahora(QuartaperguntaAguaCasa.hourxxxx);
    p.setQuartaminu(QuartaperguntaAguaCasa.minutexxxx);
    p.setQuintahora(QuintaperguntaAguaCasa.hourxxxxx);
    p.setQuintaminu(QuintaperguntaAguaCasa.minutexxxxx);
    Notificacao.GerarNotificacao(this, System.currentTimeMillis(), EscolhaAguaCasa.hourx, EscolhaAguaCasa.minutex);
}

public static class Notificacao {
    public static void GerarNotificacao(Context ctx, int dataInMillis, int hora, int minuto) {
        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();
        calSet.setTimeInMillis(dataInMillis);
        calSet.set(Calendar.HOUR_OF_DAY, hora);
        calSet.set(Calendar.MINUTE, minuto);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);
        setAlarm(ctx, calSet);
    }

    private static void setAlarm(Context ctx, Calendar targetCall)
    {
        Toast.makeText(ctx, "Alarm is set at" + targetCall.getTime(),
                Toast.LENGTH_LONG).show();
        Intent intent = new Intent(ctx, AlarmReceiver.class);

        PendingIntent pendingintent = PendingIntent.getBroadcast(ctx, 1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCall.getTimeInMillis(), pendingintent);
    }
}

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(arg0)
                .setSmallIcon(R.drawable.casa)
                .setContentTitle("ads")
                .setContentText("sad")
                .setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());

        Calendar calHoje = Calendar.getInstance();
        calHoje.setTimeInMillis(System.currentTimeMillis());
        calHoje.add(Calendar.DAY_OF_MONTH, 1);

        Notificacao.GerarNotificacao(arg0, calHoje.getTimeInMillis(), EscolhaAguaCasa.hourx, EscolhaAguaCasa.minutex);
    }
}

Note that I've created a static class to generate the notification, but it's up to you to implement it. The way I did, when the system notifies the user it automatically creates a new alarm for a day later.

    
09.07.2018 / 22:20