View notification every day

1

I want to show a notification every day at the same time, I was able to show a notification at a time, but when it arrives at the same time again it does not appear

MyActivity

Calendar cal = Calendar.getInstance();
    Date data1 = cal.getTime();
    cal.set(Calendar.HOUR_OF_DAY, 19);
    cal.set(Calendar.MINUTE, 55);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date data2 = cal.getTime();
    if (data1.compareTo(data2) != 1) {
        Intent intent = new Intent(this, AlarmReceiverActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    } else {
        Log.e("Else", "Entrou");
    }

AlarmReceiverActivity

int notificationId = 001;

    Intent viewIntent = new Intent(this, MyActivity.class);
    viewIntent.putExtra("notificacao", "2");
    PendingIntent viewPendingIntent =PendingIntent.getActivity(this, 0, viewIntent, 0);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle("")
                    .setContentText("Que tal da uma olhada nas suas tarefas agora?")
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))//RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                    .setContentIntent(viewPendingIntent);

    NotificationManagerCompat notificationManager =NotificationManagerCompat.from(this);
     notificationManager.notify(notificationId, notificationBuilder.build());
    finish();
    
asked by anonymous 14.01.2015 / 23:11

1 answer

1

Do the following in MyActivity , using AlarmManager.setRepeating(...) :

Calendar cal = Calendar.getInstance();

Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

// 24 horas em ms.
// Dica: mude o (24 * 60) para 2 e verifique se a notificação vai
// aparecer a cada 2 min, para não ter que esperar 24 horas...  
final int period = (24 * 60) * 60 * 1000; 

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), period, pendingIntent);

Make sure that you have declared the AlarmReceiverActivity service on your AndroidManifest, within the <application> tag. Example:

    
15.01.2015 / 00:47