Skip to content

Commit be9253f

Browse files
committed
fix(examples): retry on recipients=0 response
1 parent 1101b49 commit be9253f

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

examples/demo/lib/services/onesignal_api_service.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ class OneSignalApiService {
7979
// Retry while the OneSignal backend hasn't yet indexed the freshly
8080
// created subscription. The /notifications endpoint reports this race in a
8181
// few different shapes, all of which return HTTP 200:
82-
// - {"errors":{"invalid_player_ids":[...]}}
82+
// - {"id":"...","recipients":0} (user just switched, push token not yet attached)
83+
// - {"id":"...","errors":{"invalid_player_ids":[...]}}
8384
// - {"id":"","errors":["All included players are not subscribed"]}
8485
// - {"id":"","errors":[...]}
85-
// Treat any 200 response without a real notification id as transient.
86+
// Treat any 200 response with no real id, populated errors, or recipients=0 as transient.
8687
for (var attempt = 1; attempt <= maxAttempts; attempt++) {
8788
try {
8889
final response = await http.post(
@@ -123,11 +124,13 @@ class OneSignalApiService {
123124
if (decoded is! Map<String, dynamic>) return false;
124125
final id = decoded['id'];
125126
final errors = decoded['errors'];
127+
final recipients = decoded['recipients'];
126128
final hasErrors =
127129
(errors is List && errors.isNotEmpty) ||
128130
(errors is Map && errors.isNotEmpty);
129131
final missingId = id is! String || id.isEmpty;
130-
return hasErrors || missingId;
132+
final zeroRecipients = recipients is num && recipients == 0;
133+
return hasErrors || missingId || zeroRecipients;
131134
}
132135

133136
Future<bool> updateLiveActivity(

examples/demo_pods/lib/services/onesignal_api_service.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ class OneSignalApiService {
7979
// Retry while the OneSignal backend hasn't yet indexed the freshly
8080
// created subscription. The /notifications endpoint reports this race in a
8181
// few different shapes, all of which return HTTP 200:
82-
// - {"errors":{"invalid_player_ids":[...]}}
82+
// - {"id":"...","recipients":0} (user just switched, push token not yet attached)
83+
// - {"id":"...","errors":{"invalid_player_ids":[...]}}
8384
// - {"id":"","errors":["All included players are not subscribed"]}
8485
// - {"id":"","errors":[...]}
85-
// Treat any 200 response without a real notification id as transient.
86+
// Treat any 200 response with no real id, populated errors, or recipients=0 as transient.
8687
for (var attempt = 1; attempt <= maxAttempts; attempt++) {
8788
try {
8889
final response = await http.post(
@@ -123,11 +124,13 @@ class OneSignalApiService {
123124
if (decoded is! Map<String, dynamic>) return false;
124125
final id = decoded['id'];
125126
final errors = decoded['errors'];
127+
final recipients = decoded['recipients'];
126128
final hasErrors =
127129
(errors is List && errors.isNotEmpty) ||
128130
(errors is Map && errors.isNotEmpty);
129131
final missingId = id is! String || id.isEmpty;
130-
return hasErrors || missingId;
132+
final zeroRecipients = recipients is num && recipients == 0;
133+
return hasErrors || missingId || zeroRecipients;
131134
}
132135

133136
Future<bool> updateLiveActivity(

0 commit comments

Comments
 (0)