added notification for tracking function and button to reset map position to current location
time
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:fforte/screens/addCam/add_cam_main.dart';
|
||||
import 'package:fforte/screens/intro_screen.dart';
|
||||
import 'package:fforte/screens/settings.dart';
|
||||
import 'package:fforte/screens/viewEntries/view_cams.dart';
|
||||
import 'package:fforte/services/notification_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flex_color_scheme/flex_color_scheme.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
@@ -15,6 +16,12 @@ import 'l10n/l10n.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// notifications
|
||||
NotificationService().initNotification();
|
||||
|
||||
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
bool isFirstLaunch = prefs.getBool('isFirstLaunch') ?? true;
|
||||
await prefs.setString('kTage1', "28");
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'dart:math';
|
||||
|
||||
import 'package:fforte/l10n/app_localizations.dart';
|
||||
import 'package:fforte/screens/helper/snack_bar_helper.dart';
|
||||
import 'package:fforte/services/notification_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
@@ -21,6 +22,7 @@ class _TrackingState extends State<Tracking> {
|
||||
List<LatLng> pathList = [];
|
||||
StreamSubscription<Position>? positionStream;
|
||||
bool positionStreamRunning = false;
|
||||
MapController mapController = MapController();
|
||||
|
||||
Random rand = Random();
|
||||
|
||||
@@ -73,26 +75,26 @@ class _TrackingState extends State<Tracking> {
|
||||
);
|
||||
|
||||
void onTrackingStart() async {
|
||||
// // notification handling for tracking in background notification
|
||||
// PermissionStatus permissionStatus =
|
||||
// await NotificationPermissions.getNotificationPermissionStatus();
|
||||
//
|
||||
// if (permissionStatus != PermissionStatus.granted) {
|
||||
// await NotificationPermissions.requestNotificationPermissions();
|
||||
// }
|
||||
// notification handling for tracking in background notification
|
||||
await NotificationService().initNotification();
|
||||
|
||||
if (mounted)
|
||||
NotificationService().showNotification(
|
||||
title: AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
);
|
||||
|
||||
positionStream = Geolocator.getPositionStream(
|
||||
locationSettings: AndroidSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
distanceFilter: 0,
|
||||
foregroundNotificationConfig:
|
||||
mounted
|
||||
? ForegroundNotificationConfig(
|
||||
notificationTitle:
|
||||
AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
notificationText: "",
|
||||
)
|
||||
: null,
|
||||
// foregroundNotificationConfig:
|
||||
// mounted
|
||||
// ? ForegroundNotificationConfig(
|
||||
// notificationTitle:
|
||||
// AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
// notificationText: "",
|
||||
// )
|
||||
// : null,
|
||||
),
|
||||
).listen((Position? position) {
|
||||
if (position != null) {
|
||||
@@ -124,6 +126,7 @@ class _TrackingState extends State<Tracking> {
|
||||
setState(() {
|
||||
positionStreamRunning = false;
|
||||
positionStream!.cancel();
|
||||
NotificationService().deleteNotification();
|
||||
});
|
||||
},
|
||||
icon: Icon(Icons.stop_rounded),
|
||||
@@ -146,8 +149,14 @@ class _TrackingState extends State<Tracking> {
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
mapController.move(pathList.last, 16);
|
||||
},
|
||||
child: Icon(Icons.my_location),
|
||||
),
|
||||
body: FlutterMap(
|
||||
mapController: MapController(),
|
||||
mapController: mapController,
|
||||
options: MapOptions(
|
||||
interactionOptions: const InteractionOptions(
|
||||
flags:
|
||||
|
||||
45
lib/services/notification_service.dart
Normal file
45
lib/services/notification_service.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
class NotificationService {
|
||||
final notifiactionPlugin = FlutterLocalNotificationsPlugin();
|
||||
|
||||
bool _isInitialized = false;
|
||||
|
||||
bool get isInitialized => _isInitialized;
|
||||
|
||||
Future<void> initNotification() async {
|
||||
if (_isInitialized) return;
|
||||
|
||||
await notifiactionPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!.requestNotificationsPermission();
|
||||
|
||||
|
||||
const initSettingsAndroid = AndroidInitializationSettings(
|
||||
'@mipmap/ic_launcher',
|
||||
);
|
||||
|
||||
const initSettings = InitializationSettings(android: initSettingsAndroid);
|
||||
|
||||
await notifiactionPlugin.initialize(initSettings);
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
NotificationDetails notificationDetails() {
|
||||
return const NotificationDetails(
|
||||
android: AndroidNotificationDetails(
|
||||
"tracking0",
|
||||
"tracking ongoing",
|
||||
importance: Importance.low,
|
||||
priority: Priority.low,
|
||||
ongoing: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> showNotification({int id = 0, String? title}) async {
|
||||
return notifiactionPlugin.show(id, title, "", notificationDetails());
|
||||
}
|
||||
|
||||
Future<void> deleteNotification({id = 0}) async {
|
||||
await notifiactionPlugin.cancel(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user