transistorsoft/flutter_background_fetch

[BUG] Error background device

Closed this issue · 3 comments

Your Environment

  • Plugin version: 15.0
  • Platform: iOS
  • OS version: iOS
  • Device manufacturer / model: device iphone 8plust
  • Flutter info (flutter info, flutter doctor):
  • Plugin config

To Reproduce
Steps to reproduce the behavior:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
}

@pragma('vm:entry-point')
void backgroundFetchHeadlessTask(HeadlessTask task) async {
var taskId = task.taskId;
var timeout = task.timeout;
if (timeout) {
print("[BackgroundFetch] Headless task timed-out: $taskId");
BackgroundFetch.finish(taskId);
return;
}

print("[BackgroundFetch] Headless event received: $taskId");

// if (taskId == 'flutter_background_fetch') {
// BackgroundFetch.scheduleTask(TaskConfig(
// taskId: "com.transistorsoft.customtask",
// delay: 5000,
// periodic: false,
// forceAlarmManager: false,
// stopOnTerminate: false,
// enableHeadless: true));
// }
BackgroundFetch.scheduleTask(TaskConfig(
taskId: "com.transistorsoft.customtask",
delay: 5000,
periodic: false,
forceAlarmManager: false,
stopOnTerminate: false,
enableHeadless: true));
BackgroundFetch.finish(taskId);
}

// Platform messages are asynchronous, so we initialize in an async method.
Future initPlatformState() async {
// Load persisted fetch events from SharedPreferences

// Configure BackgroundFetch.
try {
var status = await BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
forceAlarmManager: false,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
requiresBatteryNotLow: false,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.NONE),
_onBackgroundFetch,
_onBackgroundFetchTimeout);
print('[BackgroundFetch] configure success: $status');

// Schedule a "one-shot" custom-task in 10000ms.
// These are fairly reliable on Android (particularly with forceAlarmManager) but not iOS,
// where device must be powered (and delay will be throttled by the OS).
await BackgroundFetch.scheduleTask(TaskConfig(
    taskId: "com.transistorsoft.customtask",
    delay: 10000,
    periodic: false,
    forceAlarmManager: true,
    stopOnTerminate: false,
    enableHeadless: true));

} on Exception catch (e) {
print("[BackgroundFetch] configure ERROR: $e");
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
}

void _onBackgroundFetch(String taskId) async {
var prefs = await SharedPreferences.getInstance();
var timestamp = DateTime.now();
// This is the fetch-event callback.
print("[BackgroundFetch] Event received: $taskId");

if (taskId == "flutter_background_fetch") {
// Perform an example HTTP request.
}
// IMPORTANT: You must signal completion of your fetch task or the OS can punish your app
// for taking too long in the background.
BackgroundFetch.finish(taskId);
}

/// This event fires shortly before your task is about to timeout. You must finish any outstanding work and call BackgroundFetch.finish(taskId).
void _onBackgroundFetchTimeout(String taskId) {
print("[BackgroundFetch] TIMEOUT: $taskId");
BackgroundFetch.finish(taskId);
}

Debug logs

  • flutter_background_fetch failed to start, status: 1
    flutter: [BackgroundFetch] ERROR: PlatformException(1, null, 1, null)
    flutter: [BackgroundFetch] configure ERROR: PlatformException(1, null, 1, null)

Additional context
Add any other context about the problem here.

Have you disabled “Background Fetch” in your app’s Settings?

I see the same exception on some iPhone devices. Background activity is allowed on those phones and it works without problems on most devices...
Not sure if this has the same root cause...

Note: Using 1.6.1
Console log:

standard 18:08:37.572668+0200 Runner : Will add backgroundTask with taskName: , expirationHandler: <NSMallocBlock: 0x280959d00>
standard 18:08:37.572710+0200 Runner : Created background task . Assertion will be assigned soon
standard 18:08:37.572756+0200 Runner : Reusing assertion <BKSProcessAssertion: 0x283f0a3f0>
standard 18:08:37.573998+0200 Runner : Ending background task with UIBackgroundTaskIdentifier: 3
standard 18:08:37.574259+0200 Runner : Will add backgroundTask with taskName: , expirationHandler: <NSMallocBlock: 0x28095ad80>
standard 18:08:37.574309+0200 Runner : Ending task with identifier 3 and description: , expireHandler: <NSMallocBlock_: 0x280944740>
standard 18:08:37.574358+0200 Runner : Decrementing reference count for assertion used by background task with identifier 3:
standard 18:08:37.574460+0200 Runner : Created background task . Assertion will be assigned soon
standard 18:08:37.574506+0200 Runner : Reusing assertion <BKSProcessAssertion: 0x283f0a3f0>
standard 18:08:37.576050+0200 Runner : Ending background task with UIBackgroundTaskIdentifier: 4
standard 18:08:37.576114+0200 Runner : Ending task with identifier 4 and description: , expireHandler: <NSMallocBlock_: 0x280959a80>
standard 18:08:37.576306+0200 Runner : Decrementing reference count for assertion used by background task with identifier 4:
standard 18:08:37.576411+0200 Runner : Will add backgroundTask with taskName: , expirationHandler: <NSMallocBlock: 0x280919800>
standard 18:08:37.576458+0200 Runner : Created background task . Assertion will be assigned soon
standard 18:08:37.576550+0200 Runner : Reusing assertion <BKSProcessAssertion: 0x283f0a3f0>
standard 18:08:37.577570+0200 Runner : Ending background task with UIBackgroundTaskIdentifier: 5
standard 18:08:37.577627+0200 Runner : Ending task with identifier 5 and description: , expireHandler: <NSMallocBlock_: 0x280919900>
standard 18:08:37.577683+0200 Runner : Will add backgroundTask with taskName: , expirationHandler: <NSMallocBlock: 0x28095b200>
standard 18:08:37.577737+0200 Runner : Decrementing reference count for assertion used by background task with identifier 5:
standard 18:08:37.577788+0200 Runner : Created background task . Assertion will be assigned soon
standard 18:08:37.577840+0200 Runner : Reusing assertion <BKSProcessAssertion: 0x283f0a3f0>
standard 18:08:37.578603+0200 Runner : Ending background task with UIBackgroundTaskIdentifier: 6
standard 18:08:37.578660+0200 Runner : Ending task with identifier 6 and description: , expireHandler: <NSMallocBlock_: 0x280942940>
standard 18:08:37.578775+0200 Runner : Decrementing reference count for assertion used by background task with identifier 6:
standard 18:08:37.622869+0200 Runner flutter: [BackgroundFetch] ERROR: PlatformException(1, null, 1, null)
standard 18:08:37.623038+0200 Runner flutter: [2023-10-25 18:08:37.504152 | Catcher 2 | INFO] Setup localization lazily!

Sorry for the noise, it's actually really related to "Background Fetch" being disabled.
try/catch around configure solved that.