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
Http Network call is not working (Flutter Android) #171
Comments
I am having the same issue. |
Me too, any ideia? |
Can you try changing function startJobs() {
Workmanager.executeTask((task, inputData) async {
switch (task) {
case httpSync:
await httpSyncApis();
break;
}
print(
"Native called background task: $task"); //simpleTask will be emitted here.
return true;
});
} Please note the added This way, a future (not completed) is returned which will only complete after awaiting httpSyncApis to finish. My guess why yours is not working: You are returning an already completed future. This might tell the system your task has finished successfully, therefore not executing further code. As your http call is async, it might be dropped by the system. |
I am having the same issue. |
It does not work |
@trighomautumnatg: Can you post parts of your code? Without it's difficult to tell what's wrong. |
this solution is not worked
|
@yekkaplan: Same here, can you post your code? |
I have some issue. This is my code;
I can see in the console: "Alarm Control 1 - Alarm Control 2 - Alarm Control 3" but, getDataFromApi() doesn't fetch data. Actually, it doesn't request. |
First of all, please format you code. It's incredibly hard to read it like this. Try this: void callbackDispatcher() async {
Workmanager.executeTask((taskName, inputData) async {
switch (taskName) {
case myTask:
print('Alarm Control 1');
await keysControls();
print('Alarm Control 2');
if (value) {
print('Alarm Control 3');
await getDataFromApi();
}
break;
}
return Future.value(true);
});
} Using Why yours is not working? You are not awaiting the Future returned by method call Hope this helps. |
@gebsl Sorry for bad code format because, I wrote first comment in GitHub. Actually, my problem is very obvious. My app request only one http request. I want to run http request method when app destroy. If you can help me, I will be very happy. Thanks for your reply. Have a good day. |
Hi @mustafaynk: You could try to combine this (https://flutter.dev/docs/get-started/flutter-for/android-devs#how-do-i-listen-to-android-activity-lifecycle-events) with flutter_workmanager. Just run |
Hi again @gebsl ; I solved my problem. Thanks a lot. |
Hi @mustafaynk, can you state how you were able to solve the problem? Might also be interesting for other devs. Thanks! |
@gebsl Sure. |
Calldispatcher function does not called . Can you help me out |
void callbackDispatcher() async { |
Sorry, more code needed. And please format your code, it's hard to read like this. |
@gebsl Please let me know how to format my code like yours |
Hi @mustafaynk . Can you share your worked code. |
@DeepaPradeepa please markup your code like this: ```dart will then look like var x = 'yourcodegoeshere' |
void callbackDispatcher() async {
Workmanager.executeTask((taskName, inputData) async {
switch (taskName) {
case fetchBackground:
print("backgroundfetchcalled in Android");
getcurrentlocation();
break;
case Workmanager.iOSBackgroundTask:
print("backgroundfetchcalled in ios");
break;
}
return true;
});
}
void getcurrentlocation() async {
print("userLocationlatcalled");
try {
// Position userLocation = await Geolocator().getCurrentPosition(
// locationPermissionLevel: GeolocationPermission.location,desiredAccuracy:LocationAccuracy.high);
print("userLocationcalled}");
AppPreferences _appPreference;
_appPreference = App().getAppPreferences();
final prefs = await _appPreference.getAuthToken();
// String tokenVal;
LatLng userLocation = LatLng(9.98,78.11);
//);
await updateCurrentLoc1(userLocation,prefs);
} catch (e) {
print('Location not shared');
}
//Position userLocation = await Geolocator().getCurrentPosition(desiredAccuracy:LocationAccuracy.high);
}
Future updateCurrentLoc1(LatLng position,String token) async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
var android = new AndroidNotificationDetails(
'1', 'location-bg', 'fetch location in background',
playSound: false, importance: Importance.Max, priority: Priority.High);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin.show(
0, "locationtop", "${position.longitude}", platform);
GraphQLClient graphQLClientWithHeader;
HttpLink httpLink =
HttpLink(uri:"GraphQLEndPoint",
headers: <String, String>{
'auth':token,
},);
graphQLClientWithHeader = GraphQLClient(
cache: new InMemoryCache(),
link: httpLink ,
);
//print("authtoken: ${globals.authToken}");
var variables = <String, dynamic>{
"lat": position.latitude,
"lng": position.longitude
};
Map<String, dynamic> _login_result = await getMutationValues(
graphQLClientWithHeader,
"updateDriverLocation_mutation",
variables);
if (_login_result["updateDriverLocation"]["status"] == 200) {
print("location updated");
await flutterLocalNotificationsPlugin.show(
0, "locationbottom", "${position.latitude}", platform);
//sendDriverLocation1();
return "";
} else if (_login_result["updateDriverLocation"]["status"] == 500) {
print("location notupdated");
await flutterLocalNotificationsPlugin.show(
0, "locationerror", "${_login_result["updateDriverLocation"]["errorMessage"]}", platform);
// sendDriverLocation1();
return "timeout";
} else {
print("location notupdated reason ${_login_result["updateDriverLocation"]["errorMessage"]}");
await flutterLocalNotificationsPlugin.show(
0, "locationerror", "${_login_result["updateDriverLocation"]["errorMessage"]}", platform);
return _login_result["updateDriverLocation"]["errorMessage"];
}
}
void initState() {
// getcurrentlocation();
Workmanager.initialize(
callbackDispatcher,
isInDebugMode:true
);
Workmanager.registerPeriodicTask(
"1",
fetchBackground,
frequency: Duration(seconds:30),
);
} |
First of all, a frequency of 30 seconds is not supported. The minimum time between two background calls is always 15 minutes (on Android). On iOS, you can not control the frequency at all (due to platform restrictions). Secondly, put an |
@gebsl Yes I know that time interval which is defaultly taken as 15 mins. I tried with await callbackDispatcher as well. But I am getting failure status background fetch Pushnotification |
@DeepaPradeepa, have you already tried the example project to verify whether this one works or not? |
@gebsl Yes. I tried with local push notification which is worked fine. But Now API Call functionality does not work |
I just realize we are probably not talking about the same thing.
I proposed awaiting |
Sorry @gebsl I wrongly told |
Oh, your issue is on iOS? I didn't know this. This whole thread is about workmanager on Android. |
Hi ,
I am using workmanager for syncing data from server using http plugin in flutter android . but nothing is happening when http is called . below i am attaching my sample for code for reference
Sample Code :
console logs :
The text was updated successfully, but these errors were encountered: