Skip to content

Commit 4dc0df5

Browse files
Nika HassaniNikaHsn
authored andcommitted
fix(notifications): allow offline configuration
1 parent b5aca75 commit 4dc0df5

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

packages/analytics/amplify_analytics_pinpoint_dart/test/endpoint_client_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,22 @@ void main() {
333333
),
334334
);
335335
});
336+
337+
test(
338+
'updateEndpoint throws NetworkException for AWSHttpException exceptions',
339+
() async {
340+
when(() => pinpointClient.updateEndpoint(any())).thenThrow(
341+
AWSHttpException(
342+
AWSHttpRequest(method: AWSHttpMethod.post, uri: Uri()),
343+
),
344+
);
345+
346+
expect(
347+
endpointClient.updateEndpoint(),
348+
throwsA(
349+
isA<NetworkException>(),
350+
),
351+
);
352+
});
336353
});
337354
}

packages/notifications/push/amplify_push_notifications_pinpoint/lib/src/pinpoint_provider.dart

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,25 @@ class PinpointProvider implements ServiceProviderClient {
168168

169169
@override
170170
Future<void> registerDevice(String deviceToken) async {
171+
if (!_isInitialized) {
172+
_logger.error(
173+
'Pinpoint provider not configured.',
174+
);
175+
return;
176+
}
177+
_analyticsClient.endpointClient.address = deviceToken;
178+
final channelType = _getChannelType();
179+
_analyticsClient.endpointClient.channelType = channelType;
180+
_analyticsClient.endpointClient.optOut = 'NONE';
171181
try {
172-
if (!_isInitialized) {
173-
_logger.error(
174-
'Pinpoint provider not configured.',
175-
);
176-
return;
177-
}
178-
_analyticsClient.endpointClient.address = deviceToken;
179-
final channelType = _getChannelType();
180-
_analyticsClient.endpointClient.channelType = channelType;
181-
_analyticsClient.endpointClient.optOut = 'NONE';
182182
await _withUserAgent(
183183
() async => _analyticsClient.endpointClient.updateEndpoint(),
184184
);
185-
} on AWSHttpException catch (e) {
185+
// This is allow offline configuration.
186+
// `EndpointClient.updateEndpoint()` catches all exception and convert
187+
// them to `AnalyticsException`.
188+
// `AnalyticsException` converts `AWSHttpException` to `NetworkException`.
189+
} on NetworkException catch (e) {
186190
_logger.error('Network problem when registering device: ', e);
187191
}
188192
}

packages/notifications/push/amplify_push_notifications_pinpoint/test/pinpoint_provider_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,48 @@ void main() {
274274
verify(mockEndpointClient.updateEndpoint);
275275
});
276276

277+
test('registerDevice should run successfully when device is offline',
278+
() async {
279+
when(
280+
() => mockAmplifyAuthProviderRepository.getAuthProvider(
281+
APIAuthorizationType.iam.authProviderToken,
282+
),
283+
).thenReturn(awsIamAmplifyAuthProvider);
284+
when(
285+
() => mockAnalyticsClient.init(
286+
pinpointAppId: any(named: 'pinpointAppId'),
287+
region: any(named: 'region'),
288+
authProvider: any(named: 'authProvider'),
289+
),
290+
).thenAnswer((realInvocation) async {});
291+
292+
final mockEndpointClient = MockEndpointClient();
293+
294+
when(mockEndpointClient.updateEndpoint)
295+
.thenThrow(const NetworkException('message'));
296+
297+
when(
298+
() => mockAnalyticsClient.endpointClient,
299+
).thenReturn(mockEndpointClient);
300+
301+
await expectLater(
302+
pinpointProvider.init(
303+
config: notificationsPinpointConfig,
304+
authProviderRepo: mockAmplifyAuthProviderRepository,
305+
analyticsClient: mockAnalyticsClient,
306+
),
307+
completes,
308+
);
309+
310+
expect(
311+
pinpointProvider.registerDevice(
312+
'',
313+
),
314+
completes,
315+
);
316+
verify(mockEndpointClient.updateEndpoint);
317+
});
318+
277319
test('recordEvent should run successfully', () async {
278320
when(
279321
() => mockAmplifyAuthProviderRepository.getAuthProvider(

0 commit comments

Comments
 (0)