Filip
Filip

Reputation: 2411

How to fetch data periodicty faster then once on 15min in background?

Like in titile. How to fetch data periodicly faster then once on 15min with background_fetch or with something else?

Upvotes: 0

Views: 504

Answers (2)

Filip
Filip

Reputation: 2411

Ok I found a solution. This works and fetch data every 1min +/- 10sec even when app is closed.

import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:http/http.dart' as http;

FlutterLocalNotificationsPlugin localNotificationsPlugin =
FlutterLocalNotificationsPlugin();

Future fetchData() async {
  final response =
  await http.get("https://reqres.in/api/unknown/2");
  Map<String, dynamic> list = json.decode(response.body) as Map;
  print(list['data']['name']);
}

void main() async {
  await AndroidAlarmManager.initialize();
  await AndroidAlarmManager.periodic(const Duration(minutes: 1), 0, printHello);
  runApp(MyApp());
}

Upvotes: 1

Xihuny
Xihuny

Reputation: 1538

Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs.

There is no way to increase the rate which a fetch-event occurs and this plugin sets the rate to the most frequent possible — you will never receive an event faster than 15 minutes. The operating-system will automatically throttle the rate the background-fetch events occur based upon usage patterns. Eg: if user hasn't turned on their phone for a long period of time, fetch events will occur less frequently.

More info here: https://pub.dev/packages/background_fetch

Upvotes: 0

Related Questions