title: Alarm manager not working when device is locked for a while
body: <p><strong>Background</strong> </p>
  
  <p>My app gives event notifications to users. Notifications are sent by moderators. However app allows moderators to sent "Timed notifications" where they have option to sent notification for specific time ahead of time.</p>
  
  <p><strong>My logic</strong></p>
  
  <p>When moderators will send notifications, they will send specific tag. At client side, if tag is detected, device won't show notification. Instead it will use <code>AlarmManager</code> and set alarm at given time. When intent is received from <code>AlarmManager</code> at desired time, broadcast receiver will send notification.</p>
  
  <p><strong>Code</strong></p>
  
  <p><strong><em>NotificationService.java</em></strong></p>
  
  <pre><code>    //Some other code
  
      public void setTimedNotification(String notificationstring, int Seconds){
  
              // get a Calendar object with current time
              Calendar cal = Calendar.getInstance();
              // add seconds to the calendar object
              cal.add(Calendar.SECOND, Seconds);
              Intent intent = new Intent(getBaseContext(), TimedNotifications.class);
              PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 1989, intent, PendingIntent.FLAG_UPDATE_CURRENT); //1989 is some random number
  
              // Get the AlarmManager service
              AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
          } 
  </code></pre>
  
  <p><strong><em>TimedNotifications.java</em></strong></p>
  
  <pre><code>public class TimedNotifications extends BroadcastReceiver {
  
      public static final int NOTIFICATION_ID = 1;
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          int requestID = (int) System.currentTimeMillis();
          Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          Intent notificationIntent = new Intent(context, TargetClass.class);
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
          int color = context.getResources().getColor(R.color.colorPrimary);
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                  .setSmallIcon(R.mipmap.ic_launcher)
                  .setContentTitle(notificationTitle)
                  .setStyle(new NotificationCompat.BigTextStyle()
                          .bigText(notificationMessage))
                  .setColor(color)
                  .setContentText(notificationMessage).setAutoCancel(true);
          mBuilder.setSound(alarmSound);
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
  
      }
  }
  </code></pre>
  
  <p><strong><em>AndroidManifest.xml</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.XXX.ZZZ"&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
  &lt;permission
      android:name="com.PackageName.App.permission.C2D_MESSAGE"
      android:protectionLevel="signature" /&gt;
  
  &lt;uses-permission android:name="com.PackageName.App.permission.C2D_MESSAGE" /&gt;
  
  &lt;application
  
          &lt;!-- Some other activities and details --&gt;
  
    &lt;receiver
          android:name="com.PackageName.App.gcm.TimedNotifications"
          android:process=":remote"/&gt;
  
            &lt;!-- Bla bla bla --&gt;
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>This code works perfectly when device is running and screen is not locked. So I am sure service is working fine. It even gives me timed notification if device is not in sleep. This also tells that code is working fine. However is device is locked for more than 5-10 min. I don't get any notifications. I guess there is some permission problem. I have tried lot of combinations of permissions like by adding custom flag in intent , adding removing  <code>android:process=":remote"</code> , changing name of receiver class, including package name in receiver's name, checking spelling mistakes, I even tried using wakelock like shown in <a href="https://stackoverflow.com/questions/5362177/alarmmanager-and-wakelock">this</a> post :/ etc. However no luck so far. Something is missing.</p>