Saltar al contenido

Cómo crear una notificación de recordatorio

Tenemos la mejor solución que hallamos online. Nosotros esperamos que te resulte de mucha utilidad y si puedes compartir alguna mejora siéntete libre de hacerlo..

Solución:

Necesitas dos cosas:

  • AlarmManager: para programar su notificación de forma regular (diaria, semanal,…).
  • Servicio: para iniciar su notificación cuando AlarmManager se apaga.

Aquí hay un ejemplo básico:

En tu Actividad:

Intent myIntent = new Intent(this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);

Esto activará la alarma cada día a la medianoche (12 am). Puedes cambiar eso si quieres.

Ahora, crea un Servicio NotifyService y poner este código en su onCreate():

@Override
public void onCreate() 
    NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
    Intent myIntent = new Intent(this , MyActivity.class);     
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
    mNM.notify(NOTIFICATION, notification);

Y este código mostrará la notificación cuando se reciba la alarma.

¡Buena suerte!

Aquí hay un pequeño tutorial en video de YouTube sobre las notificaciones diarias. Puedes encontrar el código fuente en la descripción.

Este vídeo no está hecho por mí. Pero creo que es una ayuda rápida. Aunque recomiendo algunos cambios porque el Notification.Builder está en desuso:

1.

import android.support.v4.app.NotificationCompat;

2.

// Change: Notification mNotify = new Notification.Builder(this) to
Notification mNotify = new NotificationCompat.Builder(this)

¡Que te diviertas!

Recuerda algo, que tienes la capacidad de agregar una reseña si te ayudó.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *