I need to show a disclosure window before asking the location permissions to the users to make the app google play conformed for my flutter app.
I made the following changes but after that neither the location permission window nor the disclosure message window with the text "We are collecting location for suggesting you interesting places." are shown. I am using latest flutter version. Here are the relevant classes:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import '../../Datamodel/user_location.dart';
class LocationService {
static final LocationService _instance = LocationService._internal();
factory LocationService(BuildContext context) {
return _instance;
}
LocationService._internal({BuildContext? context}) {
if (context != null) {
getLocationOnchange(context);
}
}
Location location = Location();
final StreamController<UserLocation> _locationController =
StreamController<UserLocation>.broadcast();
StreamSubscription<LocationData>? listener;
Stream<UserLocation> get locationStream => _locationController.stream;
void getLocationOnchange(BuildContext context) async {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Location Tracking'),
content: const Text('We are collecting location for suggesting you interesting places.'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
requestLocationPermission();
},
child: Text('OK'),
),
],
);
},
);
}
void requestLocationPermission() {
location.requestService().then((value) {
location.requestPermission().then((permissionStatus) {
if (permissionStatus == PermissionStatus.granted) {
location.enableBackgroundMode(enable: true);
location.changeSettings(
interval: 60000,
accuracy: LocationAccuracy.high,
);
listener = initListener();
}
});
});
}
StreamSubscription<LocationData>? getListener() {
return listener;
}
StreamSubscription<LocationData> initListener() {
return location.onLocationChanged.listen((locationData) {
_locationController.add(UserLocation(
locationData.latitude,
locationData.longitude,
locationData.altitude,
locationData.time,
));
});
}
void dispose() {
listener?.cancel();
listener = null;
}
}
startLocation.dart:
import 'package:flutter/cupertino.dart';
import '../services/location/LocationService.dart';
import '../services/location/post_location.dart';
Future<void> startLocation(BuildContext context) async {
var listener = LocationService(context).getListener();
listener ??= LocationService(context).initListener();
LocationService(context).locationStream;
await startLocationStream(context);
}
main.dart:
import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:sdx_patient_portal/backend/services/location/LocationService.dart';
import 'package:sdx_patient_portal/backend/services/sharedPref.dart';
import 'package:sdx_patient_portal/firebase_options.dart';
import 'MyApp.dart';
Future<void> backgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
}
late AndroidNotificationChannel channel;
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
var messaging = FirebaseMessaging.instance;
await EasyLocalization.ensureInitialized();
String routeFromMessage = '';
BuildContext? appContext;
runApp(
Builder(
builder: (BuildContext context) {
appContext = context;
return Container();
},
),
);
messaging.getInitialMessage().then((message) {
if (message != null) {
routeFromMessage = "/" + message.data["route"];
}
});
await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
LocationService(appContext!).getListener()?.resume();
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
if (!kIsWeb) {
channel = const AndroidNotificationChannel(
'high_importance_channel', 'High Importance Notifications',
importance: Importance.high);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await messaging.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
if (defaultTargetPlatform == TargetPlatform.android) {
messaging.getToken().then((newToken) {
SharedPref.saveFCMToken(newToken);
});
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
messaging.requestPermission(alert: true, sound: true);
await messaging.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
messaging.getToken().then((newToken) {
SharedPref.saveFCMToken(newToken);
});
}
bool? isLoggedIn = await SharedPref.getUserLoginSharedPrefernces();
isLoggedIn ??= false;
String route = '';
if (isLoggedIn && routeFromMessage != '') {
route = routeFromMessage;
} else {
route = isLoggedIn ? '/' : '/loginScreen';
}
runApp(EasyLocalization(
supportedLocales: const [Locale('en', 'US'), Locale('es', 'ES'), Locale('nl', 'NL'), Locale('de', 'DE')],
path: 'i18n',
fallbackLocale: const Locale('en', 'US'),
child: MyApp(
homepage: route,
isLoggedIn: isLoggedIn,
isLightTheme: true
),
));
}
and init:
class PushMessaging extends StatefulWidget {
final String title;
final String homepage;
final bool? isLoggedIn;
const PushMessaging(
{Key? key, required this.homepage, required this.title, this.isLoggedIn})
: super(key: key);
@override
State<PushMessaging> createState() => _PushMessagingState();
}
class _PushMessagingState extends State<PushMessaging> {
FirebaseMessaging messaging = FirebaseMessaging.instance;
bool isLoading = false;
bool isLightTheme = true;
int steps = 0;
String version = '';
void initPermission() async {
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: true,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
if (kDebugMode) {
print('User granted permission');
}
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
if (kDebugMode) {
print('User granted provisional permission');
}
} else {
if (kDebugMode) {
print('User declined or has not accepted permission');
}
}
}
void toggleTheme() {
setState(() => isLightTheme = !isLightTheme);
}
@override
void initState() {
super.initState();
getNewVersion();
getAmountOfQuest();
PedometerService.instance.init();
startLocation(context);
initPermission();
...rest of code
2
Answers
here are the current changes that made it happen:
I removed it from main:
and here is the location service itself:
Try this :
You should give text styles as per your needs.