Android – ¿Cómo configurar una notificación para una fecha específica en el futuro?

Editar: ¡SOLUCIONADO! ¿Alguna vez has querido establecer una notificación desde una fecha específica comenzando un cierto punto en el tiempo (cuando se inicia una actividad o cuando se pulsa un botón?). Lea más para averiguar cómo:

//Set a notification in 7 days Calendar sevendayalarm = Calendar.getInstance(); sevendayalarm.add(Calendar.DATE, 7); Intent intent = new Intent(this, Receiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 001, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, sevendayalarm.getTimeInMillis(), pendingIntent); 

Aquí está el código de la clase Receiver

 public class Receiver extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Intent intent = new Intent(this, Test.class); long[] pattern = {0, 300, 0}; PendingIntent pi = PendingIntent.getActivity(this, 01234, intent, 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.depressiontest) .setContentTitle("Take Questionnaire") .setContentText("Take questionnaire for Duke Mood Study.") .setVibrate(pattern) .setAutoCancel(true); mBuilder.setContentIntent(pi); mBuilder.setDefaults(Notification.DEFAULT_SOUND); mBuilder.setAutoCancel(true); NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(01234, mBuilder.build()); } } 

Y no te olvides de agregar los permisos a continuación en el manifiesto!

 <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <service android:name=".Receiver2" android:enabled="true"> <intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter> </service> 

No te olvides de dar los siguientes permisos de manifiesto

 <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> 

Sus registros de receptor de difusión sería así

  <receiver android:name=".AlarmReceiver" > <intent-filter> <action android:name="NOTIFICATION_SERVICE" /> </intent-filter> </receiver> 

Una cosa importante a tener en cuenta: Al registrar su receptor de difusión con un Intent-Filter , debe agregar el atributo exported y establecerlo en false. Me gusta esto:

 <service android:name=".utility.AlarmReceiver" android:exported="false"> <intent-filter> <action android:name="NOTIFICATION_SERVICE" /> </intent-filter> </service> 

Otros componentes de otras aplicaciones podrán invocar o interactuar con su servicio.

Explicación completa de Google :

 android:exported Specifies whether or not components of other applications can invoke the service or interact with it — "true" if they can, and "false" if not. When the value is "false", only components of the same application or applications with the same user ID can start the service or bind to it. The default value depends on whether the service contains intent filters. The absence of any filters means that it can be invoked only by specifying its exact class name. This implies that the service is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the service is intended for external use, so the default value is "true". 
  • Eliminar notificación de la barra de notificación de otras aplicaciones
  • ¿Cómo obtener notificaciones de Android cuando se cerró la aplicación?
  • El icono de notificación de Lollipop es demasiado pequeño
  • Icono del recuento de notificaciones de Actionbar (insignia) como Google
  • Extraer el texto de la notificación de parcelable, contentView o contentIntent
  • Actualizar una notificación en curso en silencio
  • Notificación de baja energía del bluetooth
  • ¿Es una práctica común preguntar a los usuarios si desean recibir notificaciones push?
  • Icono de notificación en blanco para Android con Phonegap Build y PushPlugin
  • ¿Cómo hacer que la función de notificación de respuesta directa de Android funcione en dispositivos pre-Android N?
  • La notificación en curso en Android honeycomb tiene un comportamiento inconsistente
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.