Skip to content

Commit

Permalink
fix: remove exception during token timeout (#3939)
Browse files Browse the repository at this point in the history
* chore: remove exception during token timeout

* fix: only call _registerDevice if device token exists

* chore: update device token timeout test
  • Loading branch information
Jordan-Nelson authored Oct 20, 2023
1 parent a4fd9a8 commit 07fcfe1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,11 @@ abstract class AmplifyPushNotifications
}

Future<void> _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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<PushNotificationException>().having(
(o) => o.underlyingException,
'underlyingException',
isA<TimeoutException>(),
),
),
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',
Expand Down Expand Up @@ -589,3 +587,12 @@ void main() {
);
});
}

class InMemoryLogger implements AWSLoggerPlugin {
final List<LogEntry> logs = [];

@override
void handleLogEntry(LogEntry logEntry) {
logs.add(logEntry);
}
}

0 comments on commit 07fcfe1

Please sign in to comment.