feat: request notifications access only AFTER user action

This commit is contained in:
sBubshait 2025-07-17 10:50:38 +03:00
parent c42c4e9be7
commit 9c4ad5cdb2
2 changed files with 32 additions and 9 deletions

View File

@ -6,11 +6,7 @@ import 'services/notification_service.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
NotificationService().initialize();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(MyApp());
}
@ -61,6 +57,9 @@ class _LandingPageState extends State<LandingPage>
_showBottomSheet = true;
});
_animationController.forward();
// Request notification permissions on user action
NotificationService().requestPermissionAndSetup();
}
void _hideBottomSheet() {
@ -78,6 +77,9 @@ class _LandingPageState extends State<LandingPage>
}
void _navigateToHome() {
// Request notification permissions on user action
NotificationService().requestPermissionAndSetup();
Navigator.of(
context,
).push(MaterialPageRoute(builder: (context) => HomeScreen()));

View File

@ -31,15 +31,36 @@ class NotificationService {
}
_messaging = FirebaseMessaging.instance;
await _requestPermission();
await _subscribeToTopics();
await _setupMessageHandlers();
} catch (e) {
print('Error initializing notifications: $e');
}
}
Future<void> requestPermissionAndSetup() async {
try {
if (!kIsWeb) {
print('Notifications are only supported on web platform');
return;
}
if (!_isNotificationSupported()) {
print('Notifications are not supported in this browser');
return;
}
if (_messaging == null) {
_messaging = FirebaseMessaging.instance;
await _setupMessageHandlers();
}
await _requestPermission();
await _subscribeToTopics();
} catch (e) {
print('Error requesting notification permission: $e');
}
}
bool _isNotificationSupported() {
if (!kIsWeb) return false;