Dev-hwang / flutter_foreground_task

This plugin is used to implement a foreground service on the Android platform.
https://pub.dev/packages/flutter_foreground_task
MIT License
140 stars 105 forks source link

startForegroundService fails without returning an error #216

Closed arisAlexis closed 4 months ago

arisAlexis commented 4 months ago

I am following the examples, I init the service, check for permissions and then call the foreground service with the callback as in the examples. The call returns false without throwing any error.


//outside of class
// The callback function should always be a top-level function.
@pragma('vm:entry-point')
void startCallback() {
  print('starting callback');
  // The setTaskHandler function must be called to handle the task in the background.
  FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
}

FlutterForegroundTask.init(
      androidNotificationOptions: AndroidNotificationOptions(
        channelId: 'foreground_service',
        channelName: 'Foreground Service',
        channelDescription:
            'This notification appears when the foreground service is running.',
        channelImportance: NotificationChannelImportance.LOW,
        priority: NotificationPriority.LOW,
        iconData: NotificationIconData(
          resType: ResourceType.mipmap,
          resPrefix: ResourcePrefix.ic,
          name: 'launcher',
        ),
      ),
      iosNotificationOptions: const IOSNotificationOptions(
        showNotification: true,
        playSound: false,
      ),
      foregroundTaskOptions: const ForegroundTaskOptions(
        interval: 5000,
        autoRunOnBoot: true,
        allowWifiLock: true,
      ),
    );

    print('attempting to start service');

    try {
      bool started = await FlutterForegroundTask.startService(
        notificationTitle: 'Foreground service is running',
        notificationText: 'Tap to return to the app',
        callback: startCallback,
      );
      print(started);
    } catch (e) {
      print(e.toString());
    }