@@ -14,7 +14,6 @@ import 'package:flutter_news_app_mobile_client_full_source_code/app/services/app
1414import 'package:flutter_news_app_mobile_client_full_source_code/notifications/services/push_notification_service.dart' ;
1515import 'package:flutter_news_app_mobile_client_full_source_code/shared/extensions/extensions.dart' ;
1616import 'package:logging/logging.dart' ;
17- import 'package:uuid/uuid.dart' ;
1817
1918part 'app_event.dart' ;
2019part 'app_state.dart' ;
@@ -49,7 +48,6 @@ class AppBloc extends Bloc<AppEvent, AppState> {
4948 required DataRepository <User > userRepository,
5049 required PushNotificationService pushNotificationService,
5150 required DataRepository <InAppNotification > inAppNotificationRepository,
52- Uuid ? uuid,
5351 }) : _remoteConfigRepository = remoteConfigRepository,
5452 _appInitializer = appInitializer,
5553 _authRepository = authRepository,
@@ -58,7 +56,6 @@ class AppBloc extends Bloc<AppEvent, AppState> {
5856 _userRepository = userRepository,
5957 _inAppNotificationRepository = inAppNotificationRepository,
6058 _pushNotificationService = pushNotificationService,
61- _uuid = uuid ?? const Uuid (),
6259 _inlineAdCacheService = inlineAdCacheService,
6360 _logger = logger,
6461 super (
@@ -108,33 +105,10 @@ class AppBloc extends Bloc<AppEvent, AppState> {
108105 // Listen to raw foreground push notifications.
109106 _pushNotificationService.onMessage.listen ((payload) async {
110107 _logger.fine ('AppBloc received foreground push notification payload.' );
111- if (state.user != null ) {
112- try {
113- final newNotification = InAppNotification (
114- // Generate a random ID for the notification.
115- id: _uuid.v4 (),
116- userId: state.user! .id,
117- payload: payload,
118- createdAt: DateTime .now (),
119- );
120-
121- await _inAppNotificationRepository.create (
122- item: newNotification,
123- userId: state.user! .id,
124- );
125- _logger.info (
126- 'Successfully persisted in-app notification for user ${state .user !.id }.' ,
127- );
128- // Notify the app that a new notification has been received to update UI.
129- add (const AppInAppNotificationReceived ());
130- } catch (e, s) {
131- _logger.severe (
132- 'Failed to persist in-app notification from foreground message.' ,
133- e,
134- s,
135- );
136- }
137- }
108+ // The backend now persists the notification when it sends the push. The
109+ // client's only responsibility is to react to the incoming message
110+ // and update the UI to show an unread indicator.
111+ add (const AppInAppNotificationReceived ());
138112 });
139113 }
140114
@@ -148,7 +122,6 @@ class AppBloc extends Bloc<AppEvent, AppState> {
148122 final DataRepository <User > _userRepository;
149123 final DataRepository <InAppNotification > _inAppNotificationRepository;
150124 final PushNotificationService _pushNotificationService;
151- final Uuid _uuid;
152125 final InlineAdCacheService _inlineAdCacheService;
153126
154127 /// Handles the [AppStarted] event.
0 commit comments