Notifications repeating every day

Asked

Viewed 90 times

0

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

That’s the code I’ve got 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 the 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);
}

1 answer

0


Working the way you are working, the easiest way to do it would be to generate the new notification when the notification is triggered, more or less 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 created a static class to generate the notification, but it is up to you to implement. The way I did, when the system notifies the user it automatically creates a new alarm for a day later.

  • it is asking for a Setter on the following line or asks for static and turns a Error Loopy setAlarm(calSet); what do ?

  • I guess I didn’t put my class set Larm I’ll put it in the post

  • @aleanderrayson Probably this setAlarm is in its original code.

  • I guess I hadn’t edited him out .

  • @aleanderrayson, includes the method in the notification class

  • I had tried this yesterday but now she is with another mistake I created another question about if I can take a look . Thank you ! https://answall.com/questions/313306/como-usar-o-getsystemservice-n%C3%A3o-est%C3%A1tico

  • @aleanderrayson, I answered there, if it works, mark the answers as valid

  • gave proper thank you

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.