title: AlarmManager Notification daily 9 AM with service and broadcast
body: <p>This is method to set Alarm daily 5.15 PM </p>
  
  <p>I am calling <code>setAlarm()</code> Method , when my Application Launch first time.App opens <code>(First Launch time)</code>at that time automatic <code>Notification</code> generate and constantly firing same Notifications again n again and <code>interval time</code> around <code>2 min</code>.  </p>
  
  <p><strong>Note</strong> : <strong><em>Generating Notification is not issue.</em></strong> </p>
  
  <pre><code> public void setAlarm() {
      alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
  
      alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
  
      Calendar calendar = Calendar.getInstance();
  
      calendar.setTimeInMillis(System.currentTimeMillis());
      calendar.set(Calendar.HOUR_OF_DAY, 17);
      calendar.set(Calendar.MINUTE, 15);
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
      //alarmManager.setRepeating(AlarmManager.RTC, AlarmManager.INTERVAL_HALF_DAY, AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
  
  }
  </code></pre>
  
  <p><strong>AlarmService</strong></p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver {
  
  NotificationManager notificationManager;
  
  @Override
  public void onReceive(Context context, Intent intent) {
      Intent service1 = new Intent(context, MyAlarmService.class);
      context.startService(service1);
  }
  }
  </code></pre>
  
  <p><strong>MyAlarmSevice</strong></p>
  
  <pre><code>public class MyAlarmService extends Service {
  private static final int NOTIFICATION_ID = 1;
  private NotificationManager notificationManager;
  private PendingIntent pendingIntent;
  private ArrayList&lt;RandomQuotes&gt; randomQuotesArrayList;
  private PrefManager prefManager;
  
  @Override
  public IBinder onBind(Intent arg0) {
      return null;
  }
  
  @SuppressWarnings("static-access")
  @Override
  public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      Context context = this.getApplicationContext();
      prefManager = new PrefManager(this);
      randomQuotesArrayList = prefManager.getSharedPreferenceStringList(this, "quote");
      notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
      Intent mIntent = new Intent(this, MainActivity.class);
      pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
      builder.setVibrate(new long[]{5, 5, 5, 5, 5});
  
      builder.setLights(Color.RED, 3, 3);
      builder.setContentTitle("Quote Of the Day");
      builder.setContentText(randomQuotesArrayList.get(0).getRandomQuote() + "\n\n" + "-" + randomQuotesArrayList.get(0).getRandomQuoteAuthor());
      String longText;
      if (!randomQuotesArrayList.get(0).getRandomQuoteAuthor().isEmpty()) {
          longText = randomQuotesArrayList.get(0).getRandomQuote() + "\n\n" + " -" + randomQuotesArrayList.get(0).getRandomQuoteAuthor();
      } else {
          longText = randomQuotesArrayList.get(0).getRandomQuote() + "\n" +randomQuotesArrayList.get(0).getRandomQuoteAuthor();
      }
      builder.setStyle(new NotificationCompat.BigTextStyle().bigText(longText));
      builder.setSmallIcon(R.drawable.qt);
      builder.setContentIntent(pendingIntent);
      builder.setDefaults(Notification.DEFAULT_SOUND)
              .setDefaults(Notification.DEFAULT_ALL).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
      ;
      notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationManager.notify(NOTIFICATION_ID, builder.build());
  }
  }
  </code></pre>