Skip to content

Commit f0154a6

Browse files
Jordan-NelsonEquartey
authored andcommitted
fix: remove exception during token timeout (#3939)
* chore: remove exception during token timeout * fix: only call _registerDevice if device token exists * chore: update device token timeout test
1 parent 903f514 commit f0154a6

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

packages/notifications/push/amplify_push_notifications/lib/src/amplify_push_notifications_impl.dart

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,11 @@ abstract class AmplifyPushNotifications
341341
}
342342

343343
Future<void> _registerDeviceWhenConfigure() async {
344-
late String deviceToken;
345-
346344
try {
347345
await _hostApi.requestInitialToken();
348-
deviceToken =
346+
final deviceToken =
349347
await _bufferedTokenStream.peek.timeout(const Duration(seconds: 5));
348+
await _registerDevice(deviceToken);
350349
} on PlatformException catch (error) {
351350
// the error mostly like is the App doesn't have corresponding
352351
// capability to request a push notification device token
@@ -355,17 +354,14 @@ abstract class AmplifyPushNotifications
355354
recoverySuggestion: 'Review the underlying exception.',
356355
underlyingException: error,
357356
);
358-
} on TimeoutException catch (error) {
359-
throw PushNotificationException(
360-
'Timed out awaiting for device token.',
361-
recoverySuggestion:
362-
'This may happen when the native apps have not been correctly configured'
363-
' for push notifications, review push notification configurations'
364-
' of the native iOS and Android apps of your Flutter project.',
365-
underlyingException: error,
357+
} on TimeoutException {
358+
_logger.error(
359+
'Timed out awaiting for device token.'
360+
' This may happen when the native app has not been correctly configured'
361+
' for push notifications. Review push notification configurations'
362+
' of the native iOS and Android apps of your Flutter project.',
366363
);
367364
}
368-
await _registerDevice(deviceToken);
369365
}
370366

371367
void _foregroundNotificationListener(

packages/notifications/push/amplify_push_notifications/test/amplify_push_notifications_impl_test.dart

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import 'dart:async';
54
import 'dart:convert';
65

76
import 'package:amplify_core/amplify_core.dart';
@@ -269,21 +268,20 @@ void main() {
269268
);
270269
});
271270

272-
test('configure should fail if timed out awaiting for device token',
271+
test('configure should log an error if timed out awaiting for device token',
273272
() async {
274-
expect(
275-
plugin.configure(
276-
authProviderRepo: authProviderRepo,
277-
config: config,
278-
),
279-
throwsA(
280-
isA<PushNotificationException>().having(
281-
(o) => o.underlyingException,
282-
'underlyingException',
283-
isA<TimeoutException>(),
284-
),
285-
),
273+
when(mockPushNotificationsHostApi.getLaunchNotification()).thenAnswer(
274+
(_) async => standardAndroidPushMessage.cast(),
275+
);
276+
final loggerPlugin = InMemoryLogger();
277+
AmplifyLogger.category(Category.pushNotifications)
278+
.registerPlugin(loggerPlugin);
279+
await plugin.configure(
280+
authProviderRepo: authProviderRepo,
281+
config: config,
286282
);
283+
expect(loggerPlugin.logs.length, 1);
284+
expect(loggerPlugin.logs.first.level, LogLevel.error);
287285
});
288286
});
289287
test('should fail configure when registering device is unsuccessful',
@@ -589,3 +587,12 @@ void main() {
589587
);
590588
});
591589
}
590+
591+
class InMemoryLogger implements AWSLoggerPlugin {
592+
final List<LogEntry> logs = [];
593+
594+
@override
595+
void handleLogEntry(LogEntry logEntry) {
596+
logs.add(logEntry);
597+
}
598+
}

0 commit comments

Comments
 (0)