Skip to content

Commit aa846d9

Browse files
committed
fix(firebase_push_notification): improve push notification error handling
- Refactor error handling logic for better clarity and consistency - Add detailed comments to explain error handling decisions - Introduce nuanced error handling for different exception types: - Treat NotFoundException as a permanent failure and mark token for cleanup - Log other HTTP errors as severe but do not mark for deletion - Improve log messages with more specific information and context
1 parent b6d55f0 commit aa846d9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/src/services/firebase_push_notification_client.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class FirebasePushNotificationClient implements IPushNotificationClient {
1818
required this.projectId,
1919
required HttpClient httpClient,
2020
required Logger log,
21-
}) : _httpClient = httpClient,
22-
_log = log;
21+
}) : _httpClient = httpClient,
22+
_log = log;
2323

2424
/// The Firebase Project ID for push notifications.
2525
final String projectId;
@@ -141,21 +141,24 @@ class FirebasePushNotificationClient implements IPushNotificationClient {
141141
final token = deviceTokens[i];
142142

143143
if (result is Exception) {
144-
failedTokens.add(token);
145144
if (result is NotFoundException) {
146-
// This is an expected failure when a token is unregistered (e.g.,
147-
// app uninstalled). Log it as info for cleanup purposes.
145+
// This is the only case where we treat the token as permanently
146+
// invalid and mark it for cleanup.
147+
failedTokens.add(token);
148148
_log.info(
149149
'Batch $batchNumber/$totalBatches: Failed to send to an '
150150
'invalid/unregistered token: ${result.message}',
151151
);
152152
} else if (result is HttpException) {
153+
// For other HTTP errors (e.g., 500), we log it as severe but do
154+
// not mark the token for deletion as the error may be transient.
153155
_log.severe(
154156
'Batch $batchNumber/$totalBatches: HTTP error sending '
155157
'Firebase notification to token "$token": ${result.message}',
156158
result,
157159
);
158160
} else {
161+
// For any other unexpected exception.
159162
_log.severe(
160163
'Unexpected error sending Firebase notification to token "$token".',
161164
result,

0 commit comments

Comments
 (0)