diff --git a/mobile-app/lib/main.dart b/mobile-app/lib/main.dart index 37e092e32..2dc234ce0 100644 --- a/mobile-app/lib/main.dart +++ b/mobile-app/lib/main.dart @@ -55,6 +55,11 @@ Future main({bool testing = false}) async { runApp(const FreeCodeCampMobileApp()); + WidgetsBinding.instance.addPostFrameCallback((_) async { + await NotificationService().requestPermission(); + await DailyChallengeNotificationService().setupNotifications(); + }); + await QuickActionsService().init(); } diff --git a/mobile-app/lib/service/learn/daily_challenge_notification_service.dart b/mobile-app/lib/service/learn/daily_challenge_notification_service.dart index b62c2c00d..567c02cba 100644 --- a/mobile-app/lib/service/learn/daily_challenge_notification_service.dart +++ b/mobile-app/lib/service/learn/daily_challenge_notification_service.dart @@ -49,21 +49,11 @@ class DailyChallengeNotificationService { initializationSettings, onDidReceiveNotificationResponse: _onNotificationResponse, ); + } - bool permissionGranted = false; - if (Platform.isAndroid) { - permissionGranted = await _flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation< - AndroidFlutterLocalNotificationsPlugin>() - ?.requestNotificationsPermission() ?? - false; - } else { - // For iOS, we assume permission is granted after init - permissionGranted = true; - } + Future setupNotifications() async { + final permissionGranted = await areSystemNotificationsEnabled(); - // Auto-enable daily notifications if system permission is granted - // and user hasn't explicitly disabled them if (permissionGranted) { final prefs = await SharedPreferences.getInstance(); final hasSetPreference = diff --git a/mobile-app/lib/service/podcast/notification_service.dart b/mobile-app/lib/service/podcast/notification_service.dart index dd092cfa1..3f5c824f1 100644 --- a/mobile-app/lib/service/podcast/notification_service.dart +++ b/mobile-app/lib/service/podcast/notification_service.dart @@ -28,13 +28,17 @@ class NotificationService { ); await _flutterLocalNotificationsPlugin.initialize(initializationSettings); + } + Future requestPermission() async { if (Platform.isAndroid) { - await _flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation< - AndroidFlutterLocalNotificationsPlugin>() - ?.requestNotificationsPermission(); + return await _flutterLocalNotificationsPlugin + .resolvePlatformSpecificImplementation< + AndroidFlutterLocalNotificationsPlugin>() + ?.requestNotificationsPermission() ?? + false; } + return true; } Future showNotification(String title, String body) async {