Skip to content

Commit faf965e

Browse files
fix: [SDK-4788] handle notification demo failures
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ee49195 commit faf965e

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

examples/plugin-local-notif/MainPage.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ public MainPage()
3535
LineBreakMode = LineBreakMode.WordWrap,
3636
};
3737

38-
OneSignal.Notifications.PermissionChanged += OnPermissionChanged;
39-
OneSignal.User.PushSubscription.Changed += OnPushSubscriptionChanged;
40-
OneSignal.User.Changed += OnUserChanged;
38+
if (DotEnv.HasOneSignalAppId)
39+
{
40+
OneSignal.Notifications.PermissionChanged += OnPermissionChanged;
41+
OneSignal.User.PushSubscription.Changed += OnPushSubscriptionChanged;
42+
OneSignal.User.Changed += OnUserChanged;
43+
}
4144

4245
var requestOneSignalPermissionButton = new Button
4346
{
@@ -108,6 +111,10 @@ public MainPage()
108111
{
109112
await SendSimpleOneSignalNotificationAsync();
110113
}
114+
catch (Exception exception)
115+
{
116+
SetStatus($"OneSignal notification failed: {exception.Message}");
117+
}
111118
finally
112119
{
113120
showOneSignalNotificationButton.IsEnabled = true;
@@ -121,6 +128,12 @@ public MainPage()
121128
};
122129
clearButton.Clicked += (s, e) =>
123130
{
131+
if (!DotEnv.HasOneSignalAppId)
132+
{
133+
SetStatus("Set ONESIGNAL_APP_ID in .env before clearing OneSignal notifications.");
134+
return;
135+
}
136+
124137
OneSignal.Notifications.ClearAllNotifications();
125138
SetStatus("Cleared delivered notifications through OneSignal.");
126139
};
@@ -180,11 +193,23 @@ protected override void OnAppearing()
180193

181194
private void RefreshPermissionLabel()
182195
{
196+
if (!DotEnv.HasOneSignalAppId)
197+
{
198+
_permissionLabel.Text = "OneSignal permission: unavailable (app ID not set)";
199+
return;
200+
}
201+
183202
_permissionLabel.Text = $"OneSignal permission: {OneSignal.Notifications.Permission}";
184203
}
185204

186205
private void RefreshPushInfoLabel()
187206
{
207+
if (!DotEnv.HasOneSignalAppId)
208+
{
209+
_pushInfoLabel.Text = "OneSignal push subscription: unavailable (app ID not set)";
210+
return;
211+
}
212+
188213
var pushSubscription = OneSignal.User.PushSubscription;
189214
_pushInfoLabel.Text =
190215
$"OneSignal ID:\n{FormatValue(OneSignal.User.OneSignalId)}\n"

0 commit comments

Comments
 (0)