fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
851 stars 260 forks source link

Workmanager registerOneOffTask does not start on iOS #448

Closed maxmitz closed 1 year ago

maxmitz commented 1 year ago

Describe the error When I want to run the Workmanager on a real iOs device iPhone 14 I run into this error:

bgTaskSchedulingFailed(Error Domain=BGTaskSchedulerErrorDomain Code=3 "(null)") error

I did the setup as described to the best of my knowledge.

Did I do something wrong or is it a Workmanager problem?

Output of flutter doctor -v

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.3.9, on macOS 13.0 22A380 darwin-arm, locale de-DE) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 14.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.3) [✓] VS Code (version 1.73.1) [✓] Connected device (4 available) [✓] HTTP Host Availability

Here is my code for reproduction:

main.dart

import 'package:flutter/material.dart';
import 'package:workmanager/workmanager.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
  Workmanager().initialize(
    callbackDispatcher, 
    isInDebugMode: true 
  );
  Workmanager().registerOneOffTask("com.example.testWorkmanager", "com.example.testWorkmanager");

  runApp(const MyApp());

}

@pragma('vm:entry-point') 
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    print("Native called background task: $task"); 
    return Future.value(true);
  });
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), 
    );
  }
}
maxmitz commented 1 year ago

The issue was not correctly described by my side. I have to redo it with another example.