enableNotifications method
Enables notifications at the app level.
This is a thin wrapper that:
- Sets the app-level notifications enabled flag to true
- Re-checks permission status
- Requests permission if needed (may show system prompt)
- Fetches a fresh FCM token and syncs to backend
- Restarts the token refresh listener
Returns a NotificationInitResult indicating the outcome.
Example:
// In your settings screen
final result = await notificationService.enableNotifications();
if (result == NotificationInitResult.success) {
setState(() => notificationsEnabled = true);
}
Implementation
Future<NotificationInitResult> enableNotifications() async {
logd('Enabling notifications at app level');
// Set app-level flag first
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_keyNotificationsEnabled, true);
// Reset initialization state so initializeNotifications can run
_hasFcmTokenInitialized = false;
// Use the existing init flow
final result = await initializeNotifications();
if (result == NotificationInitResult.success ||
result == NotificationInitResult.alreadyInitialized) {
logi('Notifications enabled at app level');
} else {
logw('Failed to enable notifications: $result');
// Revert the flag if we couldn't enable
await prefs.setBool(_keyNotificationsEnabled, false);
}
return result;
}