diff --git a/packages/notifications/push/amplify_push_notifications/lib/src/amplify_push_notifications_impl.dart b/packages/notifications/push/amplify_push_notifications/lib/src/amplify_push_notifications_impl.dart index ae660595b6..eaf8faba52 100644 --- a/packages/notifications/push/amplify_push_notifications/lib/src/amplify_push_notifications_impl.dart +++ b/packages/notifications/push/amplify_push_notifications/lib/src/amplify_push_notifications_impl.dart @@ -341,12 +341,11 @@ abstract class AmplifyPushNotifications } Future _registerDeviceWhenConfigure() async { - late String deviceToken; - try { await _hostApi.requestInitialToken(); - deviceToken = + final deviceToken = await _bufferedTokenStream.peek.timeout(const Duration(seconds: 5)); + await _registerDevice(deviceToken); } on PlatformException catch (error) { // the error mostly like is the App doesn't have corresponding // capability to request a push notification device token @@ -355,17 +354,14 @@ abstract class AmplifyPushNotifications recoverySuggestion: 'Review the underlying exception.', underlyingException: error, ); - } on TimeoutException catch (error) { - throw PushNotificationException( - 'Timed out awaiting for device token.', - recoverySuggestion: - 'This may happen when the native apps have not been correctly configured' - ' for push notifications, review push notification configurations' - ' of the native iOS and Android apps of your Flutter project.', - underlyingException: error, + } on TimeoutException { + _logger.error( + 'Timed out awaiting for device token.' + ' This may happen when the native app has not been correctly configured' + ' for push notifications. Review push notification configurations' + ' of the native iOS and Android apps of your Flutter project.', ); } - await _registerDevice(deviceToken); } void _foregroundNotificationListener( diff --git a/packages/notifications/push/amplify_push_notifications/test/amplify_push_notifications_impl_test.dart b/packages/notifications/push/amplify_push_notifications/test/amplify_push_notifications_impl_test.dart index a805064ad8..4982136ffa 100644 --- a/packages/notifications/push/amplify_push_notifications/test/amplify_push_notifications_impl_test.dart +++ b/packages/notifications/push/amplify_push_notifications/test/amplify_push_notifications_impl_test.dart @@ -1,7 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import 'dart:async'; import 'dart:convert'; import 'package:amplify_core/amplify_core.dart'; @@ -269,21 +268,20 @@ void main() { ); }); - test('configure should fail if timed out awaiting for device token', + test('configure should log an error if timed out awaiting for device token', () async { - expect( - plugin.configure( - authProviderRepo: authProviderRepo, - config: config, - ), - throwsA( - isA().having( - (o) => o.underlyingException, - 'underlyingException', - isA(), - ), - ), + when(mockPushNotificationsHostApi.getLaunchNotification()).thenAnswer( + (_) async => standardAndroidPushMessage.cast(), + ); + final loggerPlugin = InMemoryLogger(); + AmplifyLogger.category(Category.pushNotifications) + .registerPlugin(loggerPlugin); + await plugin.configure( + authProviderRepo: authProviderRepo, + config: config, ); + expect(loggerPlugin.logs.length, 1); + expect(loggerPlugin.logs.first.level, LogLevel.error); }); }); test('should fail configure when registering device is unsuccessful', @@ -589,3 +587,12 @@ void main() { ); }); } + +class InMemoryLogger implements AWSLoggerPlugin { + final List logs = []; + + @override + void handleLogEntry(LogEntry logEntry) { + logs.add(logEntry); + } +}