Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run background tasks when the app screen is locked #371

Closed
AvishkaG opened this issue May 21, 2024 · 11 comments
Closed

Run background tasks when the app screen is locked #371

AvishkaG opened this issue May 21, 2024 · 11 comments
Labels

Comments

@AvishkaG
Copy link

AvishkaG commented May 21, 2024

The background task execution does not work for me when the device is locked for both iOS and Android as well. (Screen lock).

Plugin version: background_fetch: ^1.3.4
Platform: iOS/ Android
OS version: Android 10
Device manufacturer / model: Vivo 1806

This is my code.

import 'dart:io';

import 'package:background_task_app/network_service.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:background_fetch/background_fetch.dart';

Future<void> performTask() async {
  await storeTime("Your Name");
}

void main() {
  runApp(const MainApp());
  // Register background fetch headless task.
  if (Platform.isAndroid) {
    BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
  }
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    // Configure background fetch
    configureBackgroundFetch();

    if (Platform.isIOS) {
      BackgroundFetch.scheduleTask(
        TaskConfig(
          taskId: 'com.transistorsoft.customtask',
          delay: 5000,
          periodic: true,
        ),
      );
    }

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Column(
            children: [
              Text("Mode: ${kDebugMode.toString()}"),
              const ElevatedButton(
                  onPressed: performTask, child: Text("Submit")),
            ],
          ),
        ),
      ),
    );
  }
}

// This function is called by `background_fetch` periodically.
void backgroundFetchHeadlessTask(HeadlessTask task) async {
  var taskId = task.taskId;
  bool isTimeout = task.timeout;
  if (isTimeout) {
    // This task has exceeded its allowed running-time.
    BackgroundFetch.finish(taskId);
    return;
  }
  // Perform your background task here
  await performTask();
  BackgroundFetch.finish(taskId);
}

void onBackgroundFetch(String taskId) async {
  // This is the fetch-event callback.
  print("[BackgroundFetch] Event received: $taskId");
  await performTask();
  BackgroundFetch.finish(taskId);
}

void onBackgroundFetchTimeout(String taskId) async {
  // This is the fetch-event timeout callback.
  print("[BackgroundFetch] TIMEOUT: $taskId");
  BackgroundFetch.finish(taskId);
}

void configureBackgroundFetch() async {
  await performTask();
  BackgroundFetch.configure(
    BackgroundFetchConfig(
      minimumFetchInterval: 15,
      stopOnTerminate: false,
      enableHeadless: true,
      requiresBatteryNotLow: false,
      requiresCharging: false,
      requiresStorageNotLow: false,
      requiresDeviceIdle: false,
      requiredNetworkType: NetworkType.ANY,
    ),
    onBackgroundFetch,
    onBackgroundFetchTimeout,
  );
}
@christocracy
Copy link
Member

Are you simulating events?

@AvishkaG
Copy link
Author

Yes I am doing an api POST call in this code block.

Future<void> performTask() async {
  await storeTime("Your Name");
}

@christocracy
Copy link
Member

Are you simulating events as directed in the readme?

@AvishkaG
Copy link
Author

Yes I did this for iOS as directed in the read me. But this periodic thing is not working for me even in the Android during the screen is locked.

@christocracy
Copy link
Member

Are you observing the device logs?

@AvishkaG
Copy link
Author

No, I am not observing the device logs. Should it work even when the phone is locked?

@christocracy
Copy link
Member

Observe your logs.

Should it work even when the phone is locked?

That’s what the word “background” in the plug-in’s name implies.

@AvishkaG
Copy link
Author

Ok I will observe the device logs. But I tried with another device as well and it either didn't work.

@christocracy
Copy link
Member

I suggest you install the /example app here in this repo.

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Jun 21, 2024
Copy link

github-actions bot commented Jul 5, 2024

This issue was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this as completed Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants