Android Notification not fired when phone sleeps for a long time

709 views Asked by At

I have a kind of weird problem. I know that I am missing something but I don't know what.

I am adding approximatly 5 notifications per day that I create each two to three hours.

If we have the scenario below:

  1. Notification 1 is created at 8:00 and should be fired at 12:00.
  2. When Notification 1 is received, it is processed and Notification 2 is created and needs to be fired at 14:00.
  3. When Notification 2 is received, it is processed and Notification 3 is created and should be fired at 17:00
  4. When Notification 3 is received, it is processed and Notification 4 is created and should be fired at 8:00 the next Day.
  5. Notification 4 will not be received until I manually unlock/unsleep the phone. The time of the notification is 8:00 even if I unlocked the phone at 9:00.

So my problem is when the phone goes to sleep for a long time, the notifications are not processed until I manually pressed the wake button on my Samsung S7. Also, I am playing a media each time the notification is received thru a Service. For Notification 4 the media does not play until I manually wake the phone. Also, I have some style applied to the content, It displays correctly for Notification 1,2 and 3 but not on Notification 4.

I thought that maybe when I am creating Notifications for the next day, there is a problem registering them so I tried to play with my date manually and everything works ok.

This is how I create my Notification:

public Notification getNotification(String notificationName, long notificationTime) {

    Intent intent = new Intent( context, NotificationPlayerService.class );
    intent.setAction( NotificationPlayerService.ACTION_STOP );
    PendingIntent pendingIntent = PendingIntent.getService(context, 1, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle("Notification");
    builder.setContentText(notificationName);
    builder.setSmallIcon(R.drawable.icon);
    builder.setColor(ContextCompat.getColor(context, R.color.teal_500));
    builder.setLights(Color.GREEN, 3000, 3000);        
    builder.setVibrate(new long[]{0,750});
    builder.setWhen(notificationTime);
    builder.setShowWhen(true);     
    Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    builder.setSound(sound);
    builder.setDeleteIntent(pendingIntent);
    return builder.build();
}

And this is how I schedule them:

@SuppressLint("NewApi")
public void scheduleNotification(Notification notification, long notificationTime, String action, int id) {

    Intent notificationIntent = new Intent(context, NotificationPublisher.class);
    notificationIntent.setAction(action);       
    if(id!=-1){         
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
    }
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);        
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);        

    if (Build.VERSION.SDK_INT > 19) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, notificationTime, pendingIntent);        
    }

}

This is how I receive them:

public void onReceive(Context context, Intent intent) {

    this.context = context;

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    String action = intent.getAction();
    int id = intent.getIntExtra(NOTIFICATION_ID, -1);
    Log.e("NOTIFICATION_RECEIVER","Notification received " + action +"-"+ id);

    if(id != -1){

        Notification notification = intent.getParcelableExtra(NOTIFICATION);                        
        notificationManager.notify(id, notification);                       

    }

    Intent serviceIntent = NotificationService.startNotificationService(context);
    if (serviceIntent != null) {
        startWakefulService(context, serviceIntent);
    }

}

And Finally I am using an IntenService to create my notifications:

@Override
protected void onHandleIntent(Intent intent) {

    try{
        startNotifications();
    }
    finally {
        WakefulBroadcastReceiver.completeWakefulIntent(intent);
    }
}

startNotification uses methods getNotification and scheduelNotification shown above. Basically it gets some settings from SharedPreferences and calculates when the notifications should be fired.

Thank you

0

There are 0 answers