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

After build apk #158

Closed
MohamadKattan opened this issue May 27, 2022 · 10 comments
Closed

After build apk #158

MohamadKattan opened this issue May 27, 2022 · 10 comments

Comments

@MohamadKattan
Copy link

MohamadKattan commented May 27, 2022

Hi, Thank u for ur work. Everything is working well, but after build apk service in background doesnt work untill i restart phone. Before restart phone background service stay in preparing mode. After restart phone everything work well. Note: After build apk and restart phone only once all functions work well. I will be very

@ekasetiawans
Copy link
Owner

hi @MohamadKattan Can you elaborate in more detail?

@MohamadKattan
Copy link
Author

Before start build app bundle in release mode I built apk on debug. Then I installed this apk on my phone for testing all functions but background service didnt work and notification phone was in preparing. I wait two hours and nothing change, service didnt start until I reset phone. Then I tested on many phones versions to make sure issue isnt in phone.

//1

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeService();
await Firebase.initializeApp();
if (defaultTargetPlatform == TargetPlatform.android) {
AndroidGoogleMapsFlutter.useAndroidViewSurface = true;
}
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then((_) {
runApp(const MyApp());
});
}

Future initializeService() async {
final service = FlutterBackgroundService();
await service.configure(
androidConfiguration: AndroidConfiguration(
onStart: onStart,
autoStart: true,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
onBackground: onIosBackground,
),
);
service.startService();
}

void onStart(ServiceInstance service) async {
await Firebase.initializeApp();
String userId = AuthSev().auth.currentUser!.uid;
DatabaseReference driverRef = FirebaseDatabase.instance.ref().child("driver");

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

service.on('setAsBackground').listen((event) {
  service.setAsBackgroundService();
});

}
service.on('stopService').listen((event) {
service.stopSelf();
});
driverRef.child(userId).child("service").set("working");

if (userId.isNotEmpty) {
await PlanDays().setIfBackgroundOrForeground(true);
await driverRef.child(userId).child("exPlan").once().then((value) {
if (value.snapshot.exists && value.snapshot.value != null) {
final snap = value.snapshot.value;
if (snap != null) {
exPlan = int.parse(snap.toString());
}
}
});
}

Timer.periodic(const Duration(seconds: 50), (timer) async {
if (exPlan == 0) {
driverRef.child(userId).child("status").once().then((value) {
if (value.snapshot.exists && value.snapshot.value != null) {
final snap = value.snapshot.value;
String _status = snap.toString();
if (_status == "checkIn" || _status == "") {
timer.cancel();
return;
}
driverRef.child(userId).child("status").set("payTime");
timer.cancel();
GeoFireSrv().makeDriverOffLine();
}
});
}
else if(exPlan>0){
exPlan = exPlan - 1;
await driverRef.child(userId).child("exPlan").set(exPlan);
await driverRef.child(userId).child("service").once().then((value) {
if (value.snapshot.value != null) {
final snap = value.snapshot.value;
String serviceWork = snap.toString();
if (serviceWork == "working") {
gotLocationInTermented(userId,driverRef);
}
} else {
return;
}
});
if (exPlan <= 0) {
driverRef.child(userId).child("status").once().then((value) {
if (value.snapshot.exists && value.snapshot.value != null) {
final snap = value.snapshot.value;
String _status = snap.toString();
if (_status == "checkIn" || _status == "") {
timer.cancel();
return;
}
driverRef.child(userId).child("status").set("payTime");
timer.cancel();
GeoFireSrv().makeDriverOffLine();
}
});
}
}
if (service is AndroidServiceInstance) {
service.setForegroundNotificationInfo(
title: "Garanti Taxi : ",
content: " working in background",
);
// test using external plugin
final deviceInfo = DeviceInfoPlugin();
String? device;
if (Platform.isAndroid) {
final androidInfo = await deviceInfo.androidInfo;
device = androidInfo.model;
}

  if (Platform.isIOS) {
    final iosInfo = await deviceInfo.iosInfo;
    device = iosInfo.model;
  }

  service.invoke(
    'update',
    {
      "current_date": DateTime.now().toIso8601String(),
      "device": device,
    },
  );
}

});
}

//2 in home screen

void initState(){
FlutterBackgroundService().invoke("setAsBackground");
super.initState();
}

@ekasetiawans
Copy link
Owner

We have to open the application at least once after the installation process is complete. It is necessary because we have to update the callback handle with the latest binary. Note that, if you run the app in debug mode using USB, the binary will not shipped to your phone until you full-restart the app (re-run your app from the IDE).

@MohamadKattan
Copy link
Author

Already i did all this Befor send to you .

@ekasetiawans
Copy link
Owner

@MohamadKattan OK, I'll give it a try when I have the chance.

@MohamadKattan
Copy link
Author

Okay . anyway thank u for your work and have a good day

@jaro0101
Copy link

Hi. I am having the same issue. I just added your plugin to my Flutter 2.0 app and followed the example, excluding the Flutter 3.0 line. Trying to run on Android through the USB debugger and Android Studio.

For me, the issue seems to be with the AndroidConfiguration not registering the callback for onStart. I am seeing errors in the log stating that after runService is called it is throwing an error that the callback handle is not found. This is leaving the background service in the "Preparing" state.

image

@MohamadKattan
Copy link
Author

MohamadKattan commented Jun 1, 2022 via email

@RicLzPt
Copy link

RicLzPt commented Jul 23, 2022

With me it's happened the same issue. I used example code that be on site pub.dev. In test with emulator or phone, work fine. I open app and it show the exemple code correctly. I see date update contantly. But, when I run "flutter build apk" and send to my phone, install and open, its show screen but circular indicator don´t close and notification show "preparing" eternaly
WhatsApp Image 2022-07-23 at 00 09 00
WhatsApp Image 2022-07-23 at 00 08 09
.

@TheauLep
Copy link

I have the same issue as @RicLzPt did u find a solution ? @MohamadKattan @jaro0101 @ekasetiawans Thanks in advance for the help !

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

5 participants