1

I am building an IOS and Android application using Flutter. My application needs to run a background task when the application is closed (let's say about every 15 minutes). Then show the notification. I am following this article, https://www.geeksforgeeks.org/background-local-notifications-in-flutter/?fbclid=IwAR0sKCVATW068AxuRIyDO693vcqwU0VTPVl1bgVkM3J3EGCHExv8P13dvz4. So, I am using this library, https://pub.dev/packages/workmanager. It is working for Android, but it is not working for IOS.

This is my code.

void main() {
  // needed if you intend to initialize in the `main` function
  WidgetsFlutterBinding.ensureInitialized();
  Workmanager().initialize(

    // The top level function, aka callbackDispatcher
      callbackDispatcher,

      // If enabled it will post a notification whenever
      // the task is running. Handy for debugging tasks
      isInDebugMode: true
  );
  // Periodic task registration
  Workmanager().registerPeriodicTask(
    "2",

    //This is the value that will be
    // returned in the callbackDispatcher
    "simplePeriodicTask",

    // When no frequency is provided
    // the default 15 minutes is set.
    // Minimum frequency is 15 min.
    // Android will automatically change
    // your frequency to 15 min
    // if you have configured a lower frequency.
    frequency: const Duration(minutes: 15),
  );

  runApp(const MyApp());
}

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {

    // initialise the plugin of flutterlocalnotifications.
    FlutterLocalNotificationsPlugin flip = FlutterLocalNotificationsPlugin();

    // app_icon needs to be a added as a drawable
    // resource to the Android head project.
    var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
    var ios = const IOSInitializationSettings();

    // initialise settings for both Android and iOS device.
    var settings = InitializationSettings(android: android, iOS: ios);
    flip.initialize(settings);
    _showNotificationWithDefaultSound(flip);
    return Future.value(true);
  });
}

Future _showNotificationWithDefaultSound(flip) async {
  var androidPlatformChannelSpecifics = const AndroidNotificationDetails("your channel id", "my channel name");
  var iOSPlatformChannelSpecifics = const IOSNotificationDetails();

  // initialise channel platform for both Android and iOS device.
  var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);

  await flip.show(0, 'GeeksforGeeks',
      'Your are one step away to connect with GeeksforGeeks',
      platformChannelSpecifics, payload: 'Default_Sound'
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

As I mentioned it is working as expected and showing notification on Android. When I run it on the IOS emulator, I am getting the following error.

An Observatory debugger and profiler on iPhone 12 Pro Max is available at: http://127.0.0.1:56183/fjCBE_KZDpw=/
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: PlatformException(unhandledMethod("registerPeriodicTask") error, Unhandled method registerPeriodicTask, null, null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      Workmanager.registerPeriodicTask (package:workmanager/src/workmanager.dart:173:7)
<asynchronous suspension>
The Flutter DevTools debugger and profiler on iPhone 12 Pro Max is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:56183/fjCBE_KZDpw=/

Basically workmanager is not working. What's wrong with my code and how can I fix it?

1
  • yu found the solution to perform the same in iOS? Commented Aug 5, 2022 at 9:34

1 Answer 1

6

That plug-in only supports one-off tasks on iOS.

The exception message tells you that the method registerPeriodicTask was unhandled.

Even if it did work on iOS, iOS doesn't support tasks scheduled at specific intervals. You can request background task dispatch but the operating system will make decisions based on things such as battery life, whether the device is awake and even the past performance of your app as to whether to actually execute a background task for your app.

Ideally rather than performing a task every 15 minutes on a mobile device you should devise some other approach; Have a server perform the task and send a notification if required, as an example.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. Is it possible to show notification at a specific given time? Like the alarm? I can’t find the one for IOS.
Yes, you can schedule delivery of a local notification
Paulw 11, how perform that in iOS ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.