title: How to cancel notification on unexpected/force app termination
body: <p>I'm creating an application which displays the <code>notification</code> of current playing song.</p>
  
  <p>The song is being played via <code>Service</code> &amp; the initiation &amp; cancellation of notification is done in the service itself.</p>
  
  <p>But If the app is terminated by some exception or if I force close it via task manager, the notification remains on the top on taskbar.</p>
  
  <p>How can I remove this.</p>
  
  <p>Below is the code:</p>
  
  <pre><code>//In Service onStartCommand(), there is a call to initiatePlayback()
  private void initiatePlayback() {
      try {
          if (mPlayer.isPlaying()) {
              mPlayer.stop();
          }
          mPlayer.reset();
          mPlayer.setDataSource(currentData);
          mPlayer.prepare();
  
          if (reqAudioFocus()) {
              mPlayer.start();
          }
  
          if (mPlayer.isPlaying()) {
              initNotification();
          }
      } catch (Exception e) {
          Toast.makeText(this,
                  "PlayTrack-&gt;initiatePlayback(): " + e.toString(),
                  Toast.LENGTH_SHORT).show();
      }
  }
  
  @Override
  public void onDestroy() {
      stopPlayback();
      mPlayer.release();
      mPlayer = null;
      super.onDestroy();
  }
  
  private void stopPlayback() {
      if (mPlayer.isPlaying()) {
          mPlayer.stop();
          mPlayer.reset();
          cancelNotification();
      }
  }
  private void cancelNotification() {
          String ns = Context.NOTIFICATION_SERVICE;
          NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
          mNotificationManager.cancel(NOTIFICATION_ID);
      }
  </code></pre>