showNotification method
Future<NotificationFailure?>
showNotification(
- NotificationPayload payload, {
- bool respectUserPreferences = true,
Shows a notification immediately with user preferences applied. Uses local repository for immediate display.
Implementation
Future<NotificationFailure?> showNotification(
NotificationPayload payload, {
bool respectUserPreferences = true,
}) async {
try {
if (!_isInitialized) {
return NotificationFailure.initialization(
details: 'Notification manager not initialized',
);
}
// Apply user preferences if enabled
NotificationPayload finalPayload = payload;
final UserPreferencesModel? userPreferences = _userPreferences;
if (respectUserPreferences && userPreferences != null) {
final NotificationPayload originalPayload = payload;
finalPayload = _applyUserPreferences(payload, userPreferences);
// Log only significant changes
if (finalPayload.silent) {
safeDebugLog(
'''π Notification "${payload.title}" set to SILENT due to user preferences''',
);
}
if (originalPayload.enableVibration && !finalPayload.enableVibration) {
safeDebugLog(
'''π³ Vibration disabled for "${payload.title}" by user preferences''',
);
}
if (originalPayload.playSound && !finalPayload.playSound) {
safeDebugLog(
'''π Sound disabled for "${payload.title}" by user preferences''',
);
}
}
// Check if notifications are globally disabled
if (respectUserPreferences &&
_userPreferences?.globalNotificationsEnabled == false) {
safeDebugLog(
'''π« SCHEDULED NOTIFICATION BLOCKED: "${payload.title}" - Notifications globally disabled''',
);
return NotificationFailure.configuration(
details: 'Notifications disabled by user preferences',
);
}
final NotificationFailure? result = await _localRepository
.showNotification(finalPayload);
if (result != null) {
safeDebugLog(
'β Failed to show notification "${payload.title}": ${result.message}',
);
}
return result;
} catch (e, stackTrace) {
safeDebugLog('π₯ Exception showing notification "${payload.title}": $e');
return NotificationFailure.delivery(
details: e.toString(),
stackTrace: stackTrace,
);
}
}