I use RTP_WAKEUP to "wake up" my device when it realizes that it has notifications to launch and I can actually send notifications in 10/15 or 20 minutes, but when I try to launch a notification in 2 hours or even days this does not happen ...
I leave here part of the code where I work out the whole process. What is wrong with this?
Class where I create the alarm, to launch the notification.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, 25);
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 07);
// Obtém um alarm manager
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(getContext().ALARM_SERVICE);
// O id a ser usado no pending intent
int id = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
Intent intent = new Intent(getContext(), CriarNotificacao.class);
intent.putExtra("id", id);
// Obtém o pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Regista o alerta no sistema.
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, 25);
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 07);
// Obtém um alarm manager
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(getContext().ALARM_SERVICE);
// O id a ser usado no pending intent
int id = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
Intent intent = new Intent(getContext(), CriarNotificacao.class);
intent.putExtra("id", id);
// Obtém o pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Regista o alerta no sistema.
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Class where I launch the notification
public class CriarNotificacao extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
int id = extras.getInt("id");
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("titulo")
.setContentText("mensagem")
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
mBuilder.setSmallIcon(R.drawable.ic_cake);
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.putExtra("id", String.valueOf(id));
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1500);
mNotificationManager.notify(id, mBuilder.build());
}
}