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

Showing "Background Service Preparing" in APK Build While Working In Debug Mode Perfectly #258

Closed
EXEIdeas opened this issue Dec 2, 2022 · 4 comments

Comments

@EXEIdeas
Copy link

EXEIdeas commented Dec 2, 2022

I am making an App where I will get a Hit from Background Service each after 5 Seconds for that my background service file is shared as below...

  • flutter_background_service: ^2.4.5
  • Flutter SDK: v3.3.7 Stable Release
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_background_service_android/flutter_background_service_android.dart';

const String actionRunningProgress = 'runningProgress';
const String actionStartRunning = 'startRunning';

Future<void> initializeService() async {
  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
}

void onStart(ServiceInstance service) {
  WidgetsFlutterBinding.ensureInitialized();

  if (service is AndroidServiceInstance) {
    service.setAsForegroundService();
    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });
    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
    service.on('stopService').listen((event) {
      service.stopSelf();
    });

    service.on(actionRunningProgress).listen((event) {
      print("Update Run Called In Backend");
    });

    service.on(actionStartRunning).listen((event) {
      int sec = 0;
      Timer timer = Timer.periodic(const Duration(seconds: 1), (timer) {
        sec++;
        service.setForegroundNotificationInfo(
          title: "Running in Progress",
          content: "Time Spent (seconds): $sec",
        );
        if (sec % 5 == 0) {
          //print("Background Hit Sent At Every 5 Sec");
          service.invoke(actionRunningProgress);
        }
      });
    });
  }
}

bool onIosBackground(ServiceInstance service) {
  WidgetsFlutterBinding.ensureInitialized();
  print('FLUTTER BACKGROUND FETCH');
  return true;
}

@override
void dispose() {
  // Remove/Dispose
}

And I am started the service in main.dart as follows

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeService();
  runApp(const MyApp());
}

And calling Service in main home.dart as follows

void initState() {
    // TODO: implement initState
   service.startService().then((value) {
      // Run BG Service Start
     service.invoke(actionStartRunning);

      // Receive Data At Foreground
     service .on(actionRunningProgress).listen((event) {
        print("Update Run Called In Frontend");
      });
    });
    super.initState();
  }

Now the problem is that everything is working until I connect my Device via Cable and Run but when I build APK then it is just showing "Background Service Preparing" in notification only.

This is completely a new project so I didn't connect it with any other service like Google, SigningKey, etc

Here is the more info...

flutter doctor --verbose
[✓] Flutter (Channel stable, 3.3.7, on macOS 13.0.1 22A400 darwin-arm, locale en-GB)
    • Flutter version 3.3.7 on channel stable at /Users/myusername/AndroidStudioProjects/flutter_v3.3.7
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e99c9c7cd9 (4 weeks ago), 2022-11-01 16:59:00 -0700
    • Engine revision 857bd6b74c
    • Dart version 2.18.4
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/myusername/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.0.1 22A400 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 108.0.5359.71

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
Process finished with exit code 0
@aqsakhan22
Copy link

See the FAQs on https://pub.dev/packages/flutter_background_service
Service not running in Release Mode #
Add @pragma('vm:entry-point') to the onStart() method. Example:

@pragma('vm:entry-point')
void onStart(ServiceInstance service){
  ...
}

@EXEIdeas
Copy link
Author

EXEIdeas commented Dec 2, 2022

Thanks... Its Working...

@EXEIdeas EXEIdeas closed this as completed Dec 2, 2022
@MoeezChohan1089
Copy link

hy sir,
Hope you are doing well i am using this pluggin but i am facing same problem that
in debug mode working perfect but in release mode not work it is showing error:

_runMain. (dart:ui/hooks.dart:129)
E/flutter ( 9887): #8 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:297)
E/flutter ( 9887): #9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192)
E/flutter ( 9887): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: No top-level
getter 'onStart1' declared.
E/flutter ( 9887): Receiver: top-level
E/flutter ( 9887): Tried calling: onStart1
E/flutter ( 9887): #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:225)
E/flutter ( 9887): #1 FfiTrampoline___getCallbackFromHandle$Method$FfiNative$Ptr (dart:ffi)
E/flutter ( 9887): #2 _getCallbackFromHandle (dart:ui/natives.dart:141)
E/flutter ( 9887): #3 PluginUtilities.getCallbackFromHandle. (dart:ui/plugins.dart:81)
E/flutter ( 9887): #4 _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:535)
E/flutter ( 9887): #5 PluginUtilities.getCallbackFromHandle (dart:ui/plugins.dart:80)
E/flutter ( 9887): #6 entrypoint (package:flutter_background_service_android/flutter_background_service_android.dart:1
6)
E/flutter ( 9887): #7 _runMain. (dart:ui/hooks.dart:129)
E/flutter ( 9887): #8 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:297)
E/flutter ( 9887): #9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192)

flutter_background_service: ^2.2.2

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.7.12, on Microsoft Windows [Version 10.0.19045.3086], locale en-PK)
[√] Windows Version (Installed version of Windows is version 10 or higher)

how to solve this issue please tell me

@EXEIdeas
Copy link
Author

hy sir, Hope you are doing well i am using this pluggin but i am facing same problem that in debug mode working perfect but in release mode not work it is showing error:

_runMain. (dart:ui/hooks.dart:129) E/flutter ( 9887): #8 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:297) E/flutter ( 9887): #9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192) E/flutter ( 9887): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: No top-level getter 'onStart1' declared. E/flutter ( 9887): Receiver: top-level E/flutter ( 9887): Tried calling: onStart1 E/flutter ( 9887): #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:225) E/flutter ( 9887): #1 FfiTrampoline___getCallbackFromHandle$Method$FfiNative$Ptr (dart:ffi) E/flutter ( 9887): #2 _getCallbackFromHandle (dart:ui/natives.dart:141) E/flutter ( 9887): #3 PluginUtilities.getCallbackFromHandle. (dart:ui/plugins.dart:81) E/flutter ( 9887): #4 _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:535) E/flutter ( 9887): #5 PluginUtilities.getCallbackFromHandle (dart:ui/plugins.dart:80) E/flutter ( 9887): #6 entrypoint (package:flutter_background_service_android/flutter_background_service_android.dart:1 6) E/flutter ( 9887): #7 _runMain. (dart:ui/hooks.dart:129) E/flutter ( 9887): #8 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:297) E/flutter ( 9887): #9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192)

flutter_background_service: ^2.2.2

Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.7.12, on Microsoft Windows [Version 10.0.19045.3086], locale en-PK) [√] Windows Version (Installed version of Windows is version 10 or higher)

how to solve this issue please tell me

Please follow this. #258 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants