Add command and fix entry

This commit is contained in:
Anton Tananaev 2025-07-01 11:32:51 -07:00
parent 36c618170e
commit 7051bb5271

View file

@ -6,10 +6,16 @@ import 'package:flutter_background_geolocation/flutter_background_geolocation.da
import 'preferences.dart';
@pragma('vm:entry-point')
Future<void> pushServiceBackgroundHandler(RemoteMessage message) async {
await Preferences.init();
await PushService._onMessage(message);
}
class PushService {
static Future<void> init() async {
await FirebaseMessaging.instance.requestPermission();
FirebaseMessaging.onBackgroundMessage(_backgroundHandler);
FirebaseMessaging.onBackgroundMessage(pushServiceBackgroundHandler);
FirebaseMessaging.onMessage.listen(_onMessage);
FirebaseMessaging.instance.onTokenRefresh.listen(_uploadToken);
bg.BackgroundGeolocation.onEnabledChange((enabled) async {
@ -19,20 +25,19 @@ class PushService {
});
}
@pragma('vm:entry-point')
static Future<void> _backgroundHandler(RemoteMessage message) async {
await Preferences.init();
await _onMessage(message);
}
static Future<void> _onMessage(RemoteMessage message) async {
final command = message.data['command'];
if (command == 'positionSingle') {
try {
await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true, extras: {'remote': true});
} catch (error) {
developer.log('Failed to get position', error: error);
}
switch (command) {
case 'positionSingle':
try {
await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true, extras: {'remote': true});
} catch (error) {
developer.log('Failed to get position', error: error);
}
case 'positionPeriodic':
await bg.BackgroundGeolocation.start();
case 'positionStop':
await bg.BackgroundGeolocation.stop();
}
}