Why 'BackgroundFetchHeadlessTask.java' does not work in ionic?

烈酒焚心 提交于 2021-01-29 07:52:38

问题


I'm working on background processes and currently using cordova's background fetch plugin in ionic. I want to run my background task even when I terminate my app. It works in iOS but the documentation says it also supports android. According to the documentation, in order to make enableHeadless: true 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.

I have also tried cordova-plugin-background-mode but it stops the background process when I terminate my app. cordova-plugin-background-fetch meets my all requirements so that's why I'm sticking with it for now. I have currently placed my java file in:

myProjectFolder/www/src/android/BackgroundFetchHeadlessTask.java

Here's my code:

JS 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, // <-- default is 15
      stopOnTerminate: false,
      enableHeadless: true
    });
  }
});

Java 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();
    }
}

config.xml

<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
        <allow-intent href="market:*" />
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
        <splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
        <splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
        <splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
        <splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
        <splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
        <splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
        <splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
        <splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
        <splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
        <splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
        <splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
        <splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" />
        <resource-file src="www/src/android/BackgroundFetchHeadlessTask.java" target="src/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java" />
</platform>

What I want to get is:

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

And what I'm getting is this:

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

Is there any solution to this problem or any other alternative to cordova-plugin-background-fetch?


回答1:


It seems that the default processing of the plug-in is being executed. Check if BackgroundFetchHeadlessTask.java under the platform has been overwritten. If it is not overwritten, the target path is incorrect. target="src/com/transistorsoft/cordova/backgroundfetch/BackgroundFetchHeadlessTask.java"



来源:https://stackoverflow.com/questions/57736107/why-backgroundfetchheadlesstask-java-does-not-work-in-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!