Skip to content

Commit b67a677

Browse files
committed
fix(examples): improve retry backoff and remove OneSignal ID row
1 parent a6e0727 commit b67a677

4 files changed

Lines changed: 20 additions & 60 deletions

File tree

examples/demo/lib/services/onesignal_api_service.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ class OneSignalApiService {
7272
return _postNotification(payload);
7373
}
7474

75-
// Retry on `invalid_player_ids` to absorb the brief race where the
76-
// subscription has been created locally but is not yet visible to the
77-
// /notifications endpoint.
7875
Future<bool> _postNotification(Map<String, dynamic> payload) async {
79-
const maxAttempts = 3;
76+
const maxAttempts = 5;
77+
int backoffMs(int n) => 2000 * (1 << (n - 1));
8078

79+
// Retry on `invalid_player_ids` to absorb the brief race where the
80+
// subscription has been created locally but is not yet visible to the
81+
// /notifications endpoint.
8182
for (var attempt = 1; attempt <= maxAttempts; attempt++) {
8283
try {
8384
final response = await http.post(
@@ -89,8 +90,8 @@ class OneSignalApiService {
8990
body: jsonEncode(payload),
9091
);
9192

92-
debugPrint('Send notification response: ${response.statusCode}');
93-
if (response.statusCode != 200) {
93+
if (response.statusCode < 200 || response.statusCode >= 300) {
94+
debugPrint('Send notification failed: ${response.body}');
9495
return false;
9596
}
9697

@@ -101,7 +102,9 @@ class OneSignalApiService {
101102
final invalidIds = errors['invalid_player_ids'];
102103
if (invalidIds is List && invalidIds.isNotEmpty) {
103104
if (attempt < maxAttempts) {
104-
await Future<void>.delayed(Duration(seconds: 3 * attempt));
105+
await Future<void>.delayed(
106+
Duration(milliseconds: backoffMs(attempt)),
107+
);
105108
continue;
106109
}
107110
debugPrint(

examples/demo/lib/widgets/sections/push_section.dart

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,6 @@ class PushSection extends StatelessWidget {
2828
padding: AppSpacing.cardPadding,
2929
child: Column(
3030
children: [
31-
Row(
32-
children: [
33-
Text(
34-
'OneSignal ID',
35-
style: Theme.of(context).textTheme.bodyMedium,
36-
),
37-
const SizedBox(width: 12),
38-
Expanded(
39-
child: Semantics(
40-
identifier: 'onesignal_id_value',
41-
container: true,
42-
child: SelectableText(
43-
vm.oneSignalId ?? '—',
44-
style: Theme.of(context).textTheme.bodySmall?.copyWith(
45-
fontFamily: 'monospace',
46-
),
47-
textAlign: TextAlign.end,
48-
),
49-
),
50-
),
51-
],
52-
),
53-
const Divider(),
5431
Row(
5532
children: [
5633
Text(

examples/demo_pods/lib/services/onesignal_api_service.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ class OneSignalApiService {
7272
return _postNotification(payload);
7373
}
7474

75-
// Retry on `invalid_player_ids` to absorb the brief race where the
76-
// subscription has been created locally but is not yet visible to the
77-
// /notifications endpoint.
7875
Future<bool> _postNotification(Map<String, dynamic> payload) async {
79-
const maxAttempts = 3;
76+
const maxAttempts = 5;
77+
int backoffMs(int n) => 2000 * (1 << (n - 1));
8078

79+
// Retry on `invalid_player_ids` to absorb the brief race where the
80+
// subscription has been created locally but is not yet visible to the
81+
// /notifications endpoint.
8182
for (var attempt = 1; attempt <= maxAttempts; attempt++) {
8283
try {
8384
final response = await http.post(
@@ -89,8 +90,8 @@ class OneSignalApiService {
8990
body: jsonEncode(payload),
9091
);
9192

92-
debugPrint('Send notification response: ${response.statusCode}');
93-
if (response.statusCode != 200) {
93+
if (response.statusCode < 200 || response.statusCode >= 300) {
94+
debugPrint('Send notification failed: ${response.body}');
9495
return false;
9596
}
9697

@@ -101,7 +102,9 @@ class OneSignalApiService {
101102
final invalidIds = errors['invalid_player_ids'];
102103
if (invalidIds is List && invalidIds.isNotEmpty) {
103104
if (attempt < maxAttempts) {
104-
await Future<void>.delayed(Duration(seconds: 3 * attempt));
105+
await Future<void>.delayed(
106+
Duration(milliseconds: backoffMs(attempt)),
107+
);
105108
continue;
106109
}
107110
debugPrint(

examples/demo_pods/lib/widgets/sections/push_section.dart

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,6 @@ class PushSection extends StatelessWidget {
2828
padding: AppSpacing.cardPadding,
2929
child: Column(
3030
children: [
31-
Row(
32-
children: [
33-
Text(
34-
'OneSignal ID',
35-
style: Theme.of(context).textTheme.bodyMedium,
36-
),
37-
const SizedBox(width: 12),
38-
Expanded(
39-
child: Semantics(
40-
identifier: 'onesignal_id_value',
41-
container: true,
42-
child: SelectableText(
43-
vm.oneSignalId ?? '—',
44-
style: Theme.of(context).textTheme.bodySmall?.copyWith(
45-
fontFamily: 'monospace',
46-
),
47-
textAlign: TextAlign.end,
48-
),
49-
),
50-
),
51-
],
52-
),
53-
const Divider(),
5431
Row(
5532
children: [
5633
Text(

0 commit comments

Comments
 (0)