26-1-20/1
This commit is contained in:
@@ -1,54 +1,74 @@
|
||||
// import 'dart:async';
|
||||
// import 'package:background_location/background_location.dart';
|
||||
// import 'package:get/get.dart';
|
||||
// import 'package:permission_handler/permission_handler.dart';
|
||||
import 'dart:io';
|
||||
|
||||
// class LocationBackgroundController extends GetxController {
|
||||
// @override
|
||||
// void onInit() {
|
||||
// super.onInit();
|
||||
// requestLocationPermission();
|
||||
// configureBackgroundLocation();
|
||||
// }
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
// Future<void> requestLocationPermission() async {
|
||||
// var status = await Permission.locationAlways.status;
|
||||
// if (!status.isGranted) {
|
||||
// await Permission.locationAlways.request();
|
||||
// }
|
||||
// }
|
||||
import 'background_service.dart';
|
||||
|
||||
// Future<void> configureBackgroundLocation() async {
|
||||
// await BackgroundLocation.setAndroidNotification(
|
||||
// title: 'Location Tracking Active'.tr,
|
||||
// message: 'Your location is being tracked in the background.'.tr,
|
||||
// icon: '@mipmap/launcher_icon',
|
||||
// );
|
||||
Future<void> requestNotificationPermission() async {
|
||||
if (Platform.isAndroid) {
|
||||
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
||||
if (androidInfo.version.sdkInt >= 33) {
|
||||
// Android 13+
|
||||
final status = await Permission.notification.request();
|
||||
if (!status.isGranted) {
|
||||
print('⚠️ إذن الإشعارات مرفوض');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BackgroundLocation.setAndroidConfiguration(3000);
|
||||
// BackgroundLocation.startLocationService();
|
||||
// BackgroundLocation.getLocationUpdates((location) {
|
||||
// // Handle location updates here
|
||||
// });
|
||||
// }
|
||||
// بعد الحصول على الإذن، ابدأ الخدمة
|
||||
await BackgroundServiceHelper.startService();
|
||||
}
|
||||
|
||||
// startBackLocation() async {
|
||||
// Timer.periodic(const Duration(seconds: 3), (timer) {
|
||||
// getBackgroundLocation();
|
||||
// });
|
||||
// }
|
||||
class PermissionsHelper {
|
||||
/// طلب إذن الإشعارات على Android 13+
|
||||
static Future<bool> requestNotificationPermission() async {
|
||||
if (Platform.isAndroid) {
|
||||
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
||||
|
||||
// getBackgroundLocation() async {
|
||||
// var status = await Permission.locationAlways.status;
|
||||
// if (status.isGranted) {
|
||||
// await BackgroundLocation.startLocationService(
|
||||
// distanceFilter: 20, forceAndroidLocationManager: true);
|
||||
// BackgroundLocation.setAndroidConfiguration(
|
||||
// Duration.microsecondsPerSecond); // Set interval to 5 seconds
|
||||
// Android 13+ (API 33+) يحتاج إذن POST_NOTIFICATIONS
|
||||
if (androidInfo.version.sdkInt >= 33) {
|
||||
final status = await Permission.notification.request();
|
||||
|
||||
// BackgroundLocation.getLocationUpdates((location1) {});
|
||||
// } else {
|
||||
// await Permission.locationAlways.request();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
if (status.isDenied) {
|
||||
print('⚠️ إذن الإشعارات مرفوض');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status.isPermanentlyDenied) {
|
||||
print('⚠️ إذن الإشعارات مرفوض بشكل دائم - افتح الإعدادات');
|
||||
await openAppSettings();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// طلب جميع الإذونات المطلوبة
|
||||
static Future<bool> requestAllPermissions() async {
|
||||
// إذن الإشعارات أولاً
|
||||
bool notificationGranted = await requestNotificationPermission();
|
||||
if (!notificationGranted) return false;
|
||||
|
||||
// إذن الموقع
|
||||
final locationStatus = await Permission.location.request();
|
||||
if (!locationStatus.isGranted) {
|
||||
print('⚠️ إذن الموقع مرفوض');
|
||||
return false;
|
||||
}
|
||||
|
||||
// إذن الموقع في الخلفية
|
||||
if (Platform.isAndroid) {
|
||||
final bgLocationStatus = await Permission.locationAlways.request();
|
||||
if (!bgLocationStatus.isGranted) {
|
||||
print('⚠️ إذن الموقع في الخلفية مرفوض');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user