title: How to cancel Android notifications?
body: <p>I am making an app where the user can set an alarm based on GPS location. I only want 1 alarm to be active at any one time. So, when the user sets a 2nd alarm, I want to cancel the notification for the 1st alarm (then set a new notification for the 2nd alarm).</p>
  
  <p>Right now, my notifications continue to stack up (as in I can't delete them, so they are all active). Here is my code where I am trying to delete the alarm and notification(s):</p>
  
  <pre><code>// Stop the location alarm activity
  Intent intentAlarmService_delete = new Intent(v.getContext(), AlarmService.class);
  stopService(intentAlarmService_delete); // I think this calls onDestroy() in AlarmService class ...
  
  mNtf = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  mNtf.cancelAll();
  
  Intent alarmIntent2 = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
  PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1, 
      alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
  pendingIntentAlarm.cancel();
  </code></pre>
  
  <p>This is the onDestroy() function in my AlarmService.class (I'm not really sure when this is called...)</p>
  
  <pre><code>public void onDestroy(){
      super.onDestroy();
      mNtf.cancel(NOTIFICATION_ID1);
  
      Intent alarmIntent = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
  
      PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1, 
          alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
      pendingIntentAlarm.cancel();
  
      Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class); 
      stopService(intentAlarmService); 
  
      mNtf.cancel(NOTIFICATION_ID1);
      mNtf.cancelAll();
  }
  </code></pre>
  
  <p>Then, this is how I am setting a new alarm and notification:</p>
  
  <pre><code>Intent intentAlarmService2 = new Intent(v.getContext(), AlarmService.class);
  startService(intentAlarmService2);
  </code></pre>
  
  <p>By the way, my AlarmService.class is working for sure.</p>
  
  <p>Thanks in advance.</p>