Fix notifications?

This commit is contained in:
Lea 2023-02-02 08:02:25 +01:00
parent 4ec79e09e0
commit 5fde56b5c9
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
2 changed files with 20 additions and 8 deletions

View file

@ -147,8 +147,10 @@ class APIRequestBuilder {
throw Exception('API Key or API URL not set');
}
final response =
await _httpClient.post(generateUrl(), body: {'api_key': api.apiKey});
var url = generateUrl();
print('Fetching $url');
final response = await _httpClient.post(url, body: {'api_key': api.apiKey});
final data = jsonDecode((response).body);
if (data['auth'] == 0) throw UnauthenticatedException(data['auth']);
return data;

View file

@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:math';
import 'package:feet/api/api.dart';
@ -38,16 +37,26 @@ void callbackDispatcher() {
return true;
}
if (latest == null || latest == 0) {
print("newestKnownPost is not set; cancelling task");
return true;
}
var api = FeverAPI(
apiKey: apiKey,
apiUrl: apiUrl,
sharedPrefs: sharedPreferences,
);
await api.request().withItems().sinceId(latest ?? 0).execute();
var response =
await api.request().withItems().sinceId(latest).execute();
var items =
api.cache.items.getAll().values.where((post) => !post.isRead);
print("Found ${items.length} new unread posts (out of "
"${api.cache.items.size} total)");
print(response);
// Only fetch feeds and favicons if necessary to avoid wasting bandwidth
if (items.isNotEmpty) {
print("New posts found, fetching feeds and favicons");
@ -100,7 +109,7 @@ void callbackDispatcher() {
void setupBackgroundTasks() async {
WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(
await Workmanager().initialize(
callbackDispatcher,
isInDebugMode: kDebugMode,
);
@ -108,9 +117,10 @@ void setupBackgroundTasks() async {
await Permission.notification.request();
// Runs every 15 minutes
Workmanager().registerPeriodicTask(
'fetch-notifications',
await Workmanager().registerPeriodicTask(
'fetch_notifications',
'fetchNotifications',
initialDelay: const Duration(seconds: 30),
constraints: Constraints(networkType: NetworkType.connected),
initialDelay: const Duration(seconds: 60),
);
}