View notification every day

Asked

Viewed 124 times

1

I’m wanting to show a notification every day at the same time, I managed to show a notification at a time , more 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();

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 you have declared the service AlarmReceiverActivity on your Androidmanifest, inside the tag <application>. Example:

Browser other questions tagged

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