Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29445

React Native background job at midnight

$
0
0

I was wondering if there is a way to run some Javascript code at midnight if the app is in background/foreground state and I found out there are some npm packages out there for React Native that lets you schedule background jobs that are executed at a set interval, but that wasn't what I was looking for so I decided to use the Headless Js API from React Native, but I couldn't get it to execute. I tried creating the service:

package com.backgroundspecificjob;import android.content.Intent;import android.os.Bundle;import com.facebook.react.HeadlessJsTaskService;import com.facebook.react.bridge.Arguments;import com.facebook.react.jstasks.HeadlessJsTaskConfig;import javax.annotation.Nullable;public class MidnightTask extends HeadlessJsTaskService {    @Override    protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {        Bundle extras = intent.getExtras();        if(extras != null) {            return new HeadlessJsTaskConfig("MidnightTask",                    Arguments.fromBundle(extras),                    5000            );        }        return null;    }}

then setting up a receiver:

package com.backgroundspecificjob;import android.app.ActivityManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;;import com.facebook.react.HeadlessJsTaskService;import java.util.List;public class MidnightReceiver extends BroadcastReceiver {    @Override    public void onReceive(final Context context, final Intent intent) {        final String action = intent.getAction();        if(!isAppOnForeground(context) && Intent.ACTION_DATE_CHANGED.equals(action)) {            Intent serviceIntent = new Intent(context, MidnightTask.class);            serviceIntent.putExtra("midnight", true);            context.startService(serviceIntent);            HeadlessJsTaskService.acquireWakeLockNow(context);        }    }    private boolean isAppOnForeground(Context context) {        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RunningAppProcessInfo> appProcesses =                activityManager.getRunningAppProcesses();        if (appProcesses == null) {            return false;        }        final String packageName = context.getPackageName();        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {            if (appProcess.importance ==                    ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&                    appProcess.processName.equals(packageName)) {                return true;            }        }        return false;    }}

and at last setting an intent as well as the service in Androidmanifest.xml:

<service      android:name=".MidnightTask"     android:enabled="true"     android:exported="true"/><receiver             android:name=".MidnightReceiver"            android:enabled="true"            android:exported="true"><intent-filter><action android:name="android.intent.action.DATE_CHANGED" /></intent-filter></receiver>

But it seems that if I change the date to 23:59 and then wait for it to be 00:00 the intent is not triggered and furthermore the service is not called. I also registered the headless task in AppRegistry:

AppRegistry.registerHeadlessTask('MidnightTask', () =>  require('./onMidnightClear'),);

I can't get the code to work and I don't quite understand why this is not working. If anyone knows the solution to my problem please let me know :)


Viewing all articles
Browse latest Browse all 29445

Trending Articles