title: Why 'BackgroundFetchHeadlessTask.java' does not work in ionic?
body: <p>I'm working on background processes and currently using <a href="https://github.com/transistorsoft/cordova-plugin-background-fetch" rel="nofollow noreferrer">cordova's background fetch plugin</a> in <strong>ionic</strong>. I want to run my background task even when I terminate my app. It works in iOS but the <a href="https://github.com/transistorsoft/cordova-plugin-background-fetch" rel="nofollow noreferrer">documentation</a> says it also supports <strong>android</strong>. According to the <a href="https://github.com/transistorsoft/cordova-plugin-background-fetch" rel="nofollow noreferrer">documentation</a>, in order to make <code>enableHeadless: true</code> to work I need to write a java code and can place the BackgroundFetchHeadlessTask.java file anywhere in my app. After creating BackgroundFetchHeadlessTask.java file I'm still getting the default implementation for headless task.</p>
  
  <p>I have also tried <a href="https://github.com/katzer/cordova-plugin-background-mode" rel="nofollow noreferrer">cordova-plugin-background-mode</a> but it stops the background process when I terminate my app. <a href="https://github.com/transistorsoft/cordova-plugin-background-fetch" rel="nofollow noreferrer">cordova-plugin-background-fetch</a> meets my all requirements so that's why I'm sticking with it for now. I have currently placed my java file in:</p>
  
  <blockquote>
    <p>myProjectFolder/www/src/android/BackgroundFetchHeadlessTask.java</p>
  </blockquote>
  
  <p>Here's my code:</p>
  
  <h1>JS code</h1>
  
  <pre><code>$ionicPlatform.ready(function(){
    var BackgroundFetch = window.BackgroundFetch;
    function background() {
      var fetchCallback = function() {
        console.log('[js] BackgroundFetch event received');
        BackgroundFetch.finish();
      };
      var failureCallback = function(error) {
        console.log('- BackgroundFetch failed', error);
      };
      BackgroundFetch.configure(fetchCallback, failureCallback, {
        minimumFetchInterval: 15, // &lt;-- default is 15
        stopOnTerminate: false,
        enableHeadless: true
      });
    }
  });
  </code></pre>
  
  <h1>Java Code</h1>
  
  <pre><code>package com.transistorsoft.cordova.backgroundfetch;
  import android.content.Context;
  import com.transistorsoft.tsbackgroundfetch.BackgroundFetch;
  import android.util.Log;
  
  public class BackgroundFetchHeadlessTask implements HeadlessTask {
      @Override
      public void onFetch(Context context) {
          Log.d(BackgroundFetch.TAG, "My BackgroundFetchHeadlessTask:  onFetch");
          // Perform your work here.
          Log.d(BackgroundFetch.TAG, "Yes, I am running");
  
          // Just as in Javascript callback, you must signal #finish
          BackgroundFetch.getInstance(context).finish();
      }
  }
  </code></pre>
  
  <h1>config.xml</h1>
  
  <pre><code>&lt;platform name="android"&gt;
          &lt;edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android"&gt;
              &lt;application android:networkSecurityConfig="@xml/network_security_config" /&gt;
          &lt;/edit-config&gt;
          &lt;resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" /&gt;
          &lt;allow-intent href="market:*" /&gt;
          &lt;icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" /&gt;
          &lt;icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" /&gt;
          &lt;icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" /&gt;
          &lt;icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" /&gt;
          &lt;icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" /&gt;
          &lt;icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" /&gt;
          &lt;splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" /&gt;
          &lt;splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" /&gt;
          &lt;splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" /&gt;
          &lt;splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" /&gt;
          &lt;splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" /&gt;
          &lt;splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" /&gt;
          &lt;splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" /&gt;
          &lt;splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" /&gt;
          &lt;splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" /&gt;
          &lt;splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" /&gt;
          &lt;splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" /&gt;
          &lt;splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" /&gt;
          &lt;resource-file src="www/src/android/BackgroundFetchHeadlessTask.java" target="src/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java" /&gt;
  &lt;/platform&gt;
  </code></pre>
  
  <p>What I want to get is:</p>
  
  <pre><code>08-31 12:13:07.722  1169  1169 D TSBackgroundFetch: - My BackgroundFetchHeadlessTask:  onFetch
  08-31 12:13:07.741  1169  1169 D TSBackgroundFetch: - Yes, I am running
  </code></pre>
  
  <p>And what I'm getting is this:</p>
  
  <pre><code>08-31 11:43:07.654 27610 27610 D TSBackgroundFetch: - Background Fetch event received
  08-31 11:43:07.665 27610 27610 D TSBackgroundFetch: - finish
  08-31 11:43:07.665 27610 27610 D TSBackgroundFetch: - jobFinished
  08-31 11:43:07.689 27610 27610 D TSBackgroundFetch: HeadlessJobService onStartJob
  08-31 11:43:07.690 27610 27610 D TSBackgroundFetch: BackgroundFetchHeadlessTask onFetch -- DEFAULT IMPLEMENTATION
  08-31 11:43:07.690 27610 27610 D TSBackgroundFetch: - finish
  08-31 11:43:07.690 27610 27610 D TSBackgroundFetch: HeadlessJobService jobFinished
  08-31 11:58:07.452 27610 27610 D TSBackgroundFetch: - Background Fetch event received
  08-31 11:58:07.452 27610 27610 D TSBackgroundFetch: - finish
  08-31 11:58:07.452 27610 27610 D TSBackgroundFetch: - jobFinished
  08-31 11:58:07.482 27610 27610 D TSBackgroundFetch: HeadlessJobService onStartJob
  08-31 11:58:07.482 27610 27610 D TSBackgroundFetch: BackgroundFetchHeadlessTask onFetch -- DEFAULT IMPLEMENTATION
  08-31 11:58:07.482 27610 27610 D TSBackgroundFetch: - finish
  
  </code></pre>
  
  <p>Is there any solution to this problem or any other alternative to cordova-plugin-background-fetch?</p>