title: Notification at certain time using a Service
body: <p>I am developing an Android prayer app for Muslims. I have already developed the code to obtain the prayer times with the help of <a href="http://praytimes.org" rel="nofollow">praytimes.org</a>. Now I want to make it so that the user is notified at that time to pray. But for now, I just want to be able to send a notification at a certain time. Looking at the code and online resources, it seems to me as if it should work but for some reason, the notification is not displaying. I know the code for the notification part is correct because I created a button to create a notification using that code and it works. So I believe the problem is with the service, receiver, or alarmManager.</p>
  
  <p>My guess is the problem has to do with the onStartCommand() method in MyNotificationService.java. I am not too familiar with services and their life-cycles so please go easy on me.</p>
  
  <p>Thanks in advance.</p>
  
  <p>In my MainActivity.java, here is the code in my onCreate() method.</p>
  
  <pre><code>/*========================Begin Notification Code==============================*/
  
  Calendar calendar = Calendar.getInstance();
  
  calendar.set(Calendar.MONTH, 1);
  calendar.set(Calendar.YEAR, 2015);
  calendar.set(Calendar.DAY_OF_MONTH, 18);
  
  calendar.set(Calendar.HOUR_OF_DAY, 4);
  calendar.set(Calendar.MINUTE, 45);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.AM_PM,Calendar.PM);
  
  Intent intent = new Intent(MainActivity.this, MyReceiver.class);
  pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);
  
  AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
  
  /*========================End Notification Code==============================*/
  </code></pre>
  
  <p>Here is MyReceiver.class</p>
  
  <pre><code>package com.ahmed.omar.tawheed;
  
  /**
   * Created by Omar on 1/18/15.
   */
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  
  public class MyReceiver extends BroadcastReceiver
  {
  
      @Override
      public void onReceive(Context context, Intent intent)
      {
          Intent service = new Intent(context, MyNotificationService.class);    
          context.startService(service);
  
      }
  }
  </code></pre>
  
  <p>Here is MyNotificationService.java</p>
  
  <pre><code>package com.ahmed.omar.tawheed;
  
  import android.app.Notification;
  import android.app.NotificationManager;
  import android.app.PendingIntent;
  import android.app.Service;
  import android.content.Context;
  import android.content.Intent;
  import android.os.IBinder;
  import android.support.v4.app.NotificationCompat;
  
  
  public class MyNotificationService extends Service  {
  
      private NotificationManager notificationManager;
      private int prayerNotificationID = 420; //blazeIt
  
      @Override
      public IBinder onBind(Intent arg0)
      {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void onCreate()
      {
          // TODO Auto-generated method stub  
          super.onCreate();
      }
  
  
      @Override
      public int onStartCommand(Intent intent1, int flag, int startId) {
  
          Intent intent = new Intent(this, MainActivity.class);
          PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
  
          Notification n  = new NotificationCompat.Builder(this)
                  .setContentTitle("Time to Pray")
                  .setContentText("It is now time to pray salah")
                  .setSmallIcon(R.drawable.ic_launcher)
                  .setContentIntent(pIntent)
                  .setAutoCancel(true)
                  .build();
  
          notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
          n.flags = Notification.FLAG_AUTO_CANCEL;
  
          // Sending the notification itself
          notificationManager.notify(prayerNotificationID, n);
  
          return START_STICKY;
      }
  
  
      @Override
      public void onDestroy()
      {
          // TODO Auto-generated method stub
          super.onDestroy();
      }
  
  }
  </code></pre>
  
  <p>Here are the lines from my AndroidManifest.xml</p>
  
  <pre><code>&lt;service android:name=".MyNotificationService"
          android:enabled="true" /&gt;
  
      &lt;receiver android:name=".MyReceiver"/&gt;
  </code></pre>