Skip to content

Commit 0c4e9c9

Browse files
committed
fix: notification permission request initilalization to postFrameCallback
1 parent 9c734c7 commit 0c4e9c9

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

mobile-app/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Future<void> main({bool testing = false}) async {
5555

5656
runApp(const FreeCodeCampMobileApp());
5757

58+
WidgetsBinding.instance.addPostFrameCallback((_) async {
59+
await NotificationService().requestPermission();
60+
await DailyChallengeNotificationService().setupNotifications();
61+
});
62+
5863
await QuickActionsService().init();
5964
}
6065

mobile-app/lib/service/learn/daily_challenge_notification_service.dart

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,11 @@ class DailyChallengeNotificationService {
4949
initializationSettings,
5050
onDidReceiveNotificationResponse: _onNotificationResponse,
5151
);
52+
}
5253

53-
bool permissionGranted = false;
54-
if (Platform.isAndroid) {
55-
permissionGranted = await _flutterLocalNotificationsPlugin
56-
.resolvePlatformSpecificImplementation<
57-
AndroidFlutterLocalNotificationsPlugin>()
58-
?.requestNotificationsPermission() ??
59-
false;
60-
} else {
61-
// For iOS, we assume permission is granted after init
62-
permissionGranted = true;
63-
}
54+
Future<void> setupNotifications() async {
55+
final permissionGranted = await areSystemNotificationsEnabled();
6456

65-
// Auto-enable daily notifications if system permission is granted
66-
// and user hasn't explicitly disabled them
6757
if (permissionGranted) {
6858
final prefs = await SharedPreferences.getInstance();
6959
final hasSetPreference =

mobile-app/lib/service/podcast/notification_service.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ class NotificationService {
2828
);
2929

3030
await _flutterLocalNotificationsPlugin.initialize(initializationSettings);
31+
}
3132

33+
Future<bool> requestPermission() async {
3234
if (Platform.isAndroid) {
33-
await _flutterLocalNotificationsPlugin
34-
.resolvePlatformSpecificImplementation<
35-
AndroidFlutterLocalNotificationsPlugin>()
36-
?.requestNotificationsPermission();
35+
return await _flutterLocalNotificationsPlugin
36+
.resolvePlatformSpecificImplementation<
37+
AndroidFlutterLocalNotificationsPlugin>()
38+
?.requestNotificationsPermission() ??
39+
false;
3740
}
41+
return true;
3842
}
3943

4044
Future<void> showNotification(String title, String body) async {

0 commit comments

Comments
 (0)