Add command and fix entry
This commit is contained in:
parent
36c618170e
commit
7051bb5271
1 changed files with 18 additions and 13 deletions
|
|
@ -6,10 +6,16 @@ import 'package:flutter_background_geolocation/flutter_background_geolocation.da
|
||||||
|
|
||||||
import 'preferences.dart';
|
import 'preferences.dart';
|
||||||
|
|
||||||
|
@pragma('vm:entry-point')
|
||||||
|
Future<void> pushServiceBackgroundHandler(RemoteMessage message) async {
|
||||||
|
await Preferences.init();
|
||||||
|
await PushService._onMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
class PushService {
|
class PushService {
|
||||||
static Future<void> init() async {
|
static Future<void> init() async {
|
||||||
await FirebaseMessaging.instance.requestPermission();
|
await FirebaseMessaging.instance.requestPermission();
|
||||||
FirebaseMessaging.onBackgroundMessage(_backgroundHandler);
|
FirebaseMessaging.onBackgroundMessage(pushServiceBackgroundHandler);
|
||||||
FirebaseMessaging.onMessage.listen(_onMessage);
|
FirebaseMessaging.onMessage.listen(_onMessage);
|
||||||
FirebaseMessaging.instance.onTokenRefresh.listen(_uploadToken);
|
FirebaseMessaging.instance.onTokenRefresh.listen(_uploadToken);
|
||||||
bg.BackgroundGeolocation.onEnabledChange((enabled) async {
|
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 {
|
static Future<void> _onMessage(RemoteMessage message) async {
|
||||||
final command = message.data['command'];
|
final command = message.data['command'];
|
||||||
if (command == 'positionSingle') {
|
switch (command) {
|
||||||
try {
|
case 'positionSingle':
|
||||||
await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true, extras: {'remote': true});
|
try {
|
||||||
} catch (error) {
|
await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true, extras: {'remote': true});
|
||||||
developer.log('Failed to get position', error: error);
|
} catch (error) {
|
||||||
}
|
developer.log('Failed to get position', error: error);
|
||||||
|
}
|
||||||
|
case 'positionPeriodic':
|
||||||
|
await bg.BackgroundGeolocation.start();
|
||||||
|
case 'positionStop':
|
||||||
|
await bg.BackgroundGeolocation.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue