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'); throw Exception('API Key or API URL not set');
} }
final response = var url = generateUrl();
await _httpClient.post(generateUrl(), body: {'api_key': api.apiKey}); print('Fetching $url');
final response = await _httpClient.post(url, body: {'api_key': api.apiKey});
final data = jsonDecode((response).body); final data = jsonDecode((response).body);
if (data['auth'] == 0) throw UnauthenticatedException(data['auth']); if (data['auth'] == 0) throw UnauthenticatedException(data['auth']);
return data; return data;

View file

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