Skip to content

Commit 5de379a

Browse files
committed
refactor(push): make notification service resilient to null clients
Updates `DefaultPushNotificationService` to accept nullable `IPushNotificationClient` instances. This change makes the service resilient to missing push notification credentials. It now checks if the configured primary provider's client was initialized before attempting to send a notification, logging a severe error and aborting if the client is unavailable. This fixes the compilation errors in `app_dependencies.dart`.
1 parent 43c748f commit 5de379a

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

lib/src/services/push_notification_service.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class DefaultPushNotificationService implements IPushNotificationService {
3535
required DataRepository<PushNotificationSubscription>
3636
pushNotificationSubscriptionRepository,
3737
required DataRepository<RemoteConfig> remoteConfigRepository,
38-
required IPushNotificationClient firebaseClient,
39-
required IPushNotificationClient oneSignalClient,
38+
required IPushNotificationClient? firebaseClient,
39+
required IPushNotificationClient? oneSignalClient,
4040
required Logger log,
4141
}) : _pushNotificationDeviceRepository = pushNotificationDeviceRepository,
4242
_pushNotificationSubscriptionRepository =
@@ -51,8 +51,8 @@ class DefaultPushNotificationService implements IPushNotificationService {
5151
final DataRepository<PushNotificationSubscription>
5252
_pushNotificationSubscriptionRepository;
5353
final DataRepository<RemoteConfig> _remoteConfigRepository;
54-
final IPushNotificationClient _firebaseClient;
55-
final IPushNotificationClient _oneSignalClient;
54+
final IPushNotificationClient? _firebaseClient;
55+
final IPushNotificationClient? _oneSignalClient;
5656
final Logger _log;
5757

5858
// Assuming a fixed ID for the RemoteConfig document
@@ -87,6 +87,26 @@ class DefaultPushNotificationService implements IPushNotificationService {
8787
'Push notifications are enabled. Primary provider is "$primaryProvider".',
8888
);
8989

90+
// Determine which client to use based on the primary provider.
91+
final IPushNotificationClient? client;
92+
if (primaryProvider == PushNotificationProvider.firebase) {
93+
client = _firebaseClient;
94+
} else {
95+
client = _oneSignalClient;
96+
}
97+
98+
// CRITICAL: Check if the selected primary provider's client was
99+
// actually initialized. If not (due to missing .env credentials),
100+
// log a severe error and abort.
101+
if (client == null) {
102+
_log.severe(
103+
'Push notifications are enabled with "$primaryProvider" as the '
104+
'primary provider, but the client could not be initialized. '
105+
'Please ensure all required environment variables for this provider are set. Aborting.',
106+
);
107+
return;
108+
}
109+
90110
// Check if breaking news notifications are enabled.
91111
final breakingNewsDeliveryConfig =
92112
pushConfig.deliveryConfigs[PushNotificationSubscriptionDeliveryType
@@ -181,11 +201,6 @@ class DefaultPushNotificationService implements IPushNotificationService {
181201
},
182202
);
183203

184-
// 8. Select the correct client and send the notifications.
185-
final client = primaryProvider == PushNotificationProvider.firebase
186-
? _firebaseClient
187-
: _oneSignalClient;
188-
189204
await client.sendBulkNotifications(
190205
deviceTokens: tokens,
191206
payload: payload,

0 commit comments

Comments
 (0)