Skip to content

Commit 4c9a510

Browse files
chore(demo): display OneSignal ID in push section
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 8c888d7 commit 4c9a510

4 files changed

Lines changed: 32 additions & 17 deletions

File tree

examples/demo/Assets/Scripts/Services/OneSignalApiService.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,14 @@ private JObject BuildBasePayload(string title, string body, string subscriptionI
180180
["contents"] = new JObject { ["en"] = body },
181181
};
182182

183-
// Retry on `invalid_player_ids` to absorb the brief race where the
184-
// subscription has been created locally but is not yet visible to the
185-
// /notifications endpoint.
183+
// Retry while the OneSignal backend hasn't yet indexed the freshly
184+
// created subscription. The /notifications endpoint reports this race
185+
// in a few different shapes, all of which return HTTP 200:
186+
// {"id":"...","recipients":0} (user just switched, push token not yet attached)
187+
// {"id":"...","errors":{"invalid_player_ids":[...]}}
188+
// {"id":"","errors":["All included players are not subscribed"]}
189+
// {"id":"","errors":[...]}
190+
// Treat any 200 response with no real id, populated errors, or recipients=0 as transient.
186191
private async Task<bool> PostNotification(string jsonPayload)
187192
{
188193
const int maxAttempts = 3;
@@ -212,7 +217,7 @@ private async Task<bool> PostNotification(string jsonPayload)
212217
var responseText = request.downloadHandler.text;
213218
request.Dispose();
214219

215-
if (HasInvalidPlayerIds(responseText))
220+
if (IsTransientSendFailure(responseText))
216221
{
217222
if (attempt < maxAttempts)
218223
{
@@ -228,15 +233,25 @@ private async Task<bool> PostNotification(string jsonPayload)
228233
return false;
229234
}
230235

231-
private static bool HasInvalidPlayerIds(string responseJson)
236+
private static bool IsTransientSendFailure(string responseJson)
232237
{
233238
if (string.IsNullOrWhiteSpace(responseJson))
234239
return false;
235240
try
236241
{
237242
var parsed = JObject.Parse(responseJson);
238-
var invalidIds = parsed["errors"]?["invalid_player_ids"] as JArray;
239-
return invalidIds != null && invalidIds.Count > 0;
243+
var id = parsed["id"]?.ToString();
244+
var errors = parsed["errors"];
245+
bool hasErrors =
246+
(errors is JArray arr && arr.Count > 0) ||
247+
(errors is JObject obj && obj.Count > 0);
248+
bool missingId = string.IsNullOrEmpty(id);
249+
var recipientsToken = parsed["recipients"];
250+
bool zeroRecipients =
251+
recipientsToken != null
252+
&& recipientsToken.Type == JTokenType.Integer
253+
&& recipientsToken.Value<long>() == 0;
254+
return hasErrors || missingId || zeroRecipients;
240255
}
241256
catch
242257
{

examples/demo/Assets/Scripts/UI/Sections/PushSectionController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private VisualElement BuildSection()
3535

3636
var pushIdRow = SectionBuilder.CreateInlineKeyValue(
3737
"Push ID",
38-
_viewModel.PushSubscriptionId ?? "—",
38+
FormatIdValue(_viewModel.PushSubscriptionId),
3939
"push_id"
4040
);
4141
_pushIdLabel = pushIdRow.Q<Label>("push_id_value");
@@ -71,14 +71,17 @@ private VisualElement BuildSection()
7171

7272
public void Refresh()
7373
{
74-
_pushIdLabel.text = _viewModel.PushSubscriptionId ?? "—";
74+
_pushIdLabel.text = FormatIdValue(_viewModel.PushSubscriptionId);
7575
_enabledToggle.SetValueWithoutNotify(_viewModel.PushOptedIn);
7676
_enabledToggle.SetEnabled(_viewModel.HasPermission);
7777
_promptButton.style.display = _viewModel.HasPermission
7878
? DisplayStyle.None
7979
: DisplayStyle.Flex;
8080
}
8181

82+
private static string FormatIdValue(string value) =>
83+
string.IsNullOrEmpty(value) ? "—" : value;
84+
8285
private void OnEnabledChanged(bool value) => _viewModel.SetPushEnabled(value);
8386
}
8487
}

examples/demo/Assets/Scripts/ViewModels/AppViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class AppViewModel : MonoBehaviour
2525
private bool _inAppMessagesPaused;
2626
private bool _locationShared;
2727
private string _externalUserId;
28+
private string _oneSignalId;
2829
private string _pushSubscriptionId;
2930
private bool _pushOptedIn;
3031
private bool _hasPermission;
@@ -58,6 +59,7 @@ public class AppViewModel : MonoBehaviour
5859
public bool InAppMessagesPaused => _inAppMessagesPaused;
5960
public bool LocationShared => _locationShared;
6061
public string ExternalUserId => _externalUserId;
62+
public string OneSignalId => _oneSignalId;
6163
public string PushSubscriptionId => _pushSubscriptionId;
6264
public bool PushOptedIn => _pushOptedIn;
6365
public bool HasPermission => _hasPermission;
@@ -118,6 +120,7 @@ public void LoadInitialState()
118120
_pushSubscriptionId = OneSignal.User.PushSubscription.Id ?? "";
119121
_pushOptedIn = OneSignal.User.PushSubscription.OptedIn;
120122
_hasPermission = OneSignal.Notifications.Permission;
123+
_oneSignalId = OneSignal.User.OneSignalId ?? "";
121124

122125
NotifyStateChanged();
123126
}
@@ -660,6 +663,8 @@ private void OnPermissionChanged(object sender, NotificationPermissionChangedEve
660663
private async void OnUserChanged(object sender, UserStateChangedEventArgs e)
661664
{
662665
Debug.Log($"[{Tag}] User changed, fetching data...");
666+
_oneSignalId = OneSignal.User.OneSignalId ?? "";
667+
NotifyStateChanged();
663668
await FetchUserDataFromApi();
664669
}
665670
}

examples/demo/Packages/packages-lock.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@
2222
"com.onesignal.unity.core": "5.2.8"
2323
}
2424
},
25-
"com.unity.asset-store-tools": {
26-
"version": "file:com.unity.asset-store-tools",
27-
"depth": 0,
28-
"source": "embedded",
29-
"dependencies": {
30-
"com.unity.nuget.newtonsoft-json": "3.2.1"
31-
}
32-
},
3325
"com.unity.nuget.newtonsoft-json": {
3426
"version": "3.2.2",
3527
"depth": 0,

0 commit comments

Comments
 (0)