Skip to content

Commit d04df3c

Browse files
committed
- improved push notifications
- downgraded Maui.Controls and Maui.Controls.Compatibility nugets from 8.0.100 to 8.0.93, due to a bug not resetting badge counter on iOS
1 parent 5e2c77b commit d04df3c

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

Spixi/Platforms/Android/SPushService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void initialize()
4444
OneSignal.Debug.AlertLevel = LogLevel.NONE;
4545

4646
OneSignal.Initialize(SPIXI.Meta.Config.oneSignalAppId);
47-
47+
4848
OneSignal.Notifications.Clicked += handleNotificationOpened;
4949
OneSignal.Notifications.WillDisplay += handleNotificationReceived;
5050

@@ -80,6 +80,7 @@ public static void clearNotifications()
8080
{
8181
clearNotificationsAfterInit = true;
8282
Logging.warn("Cannot clear notifications, OneSignal is not initialized yet.");
83+
return;
8384
}
8485
}
8586
catch (Exception e)

Spixi/Platforms/iOS/SPushService.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
using SPIXI;
77
using UIKit;
88
using UserNotifications;
9+
using OneSignalNative = Com.OneSignal.iOS.OneSignal;
910

1011
namespace Spixi
1112
{
1213
public class SPushService
1314
{
15+
private static bool isInitializing = false;
16+
private static bool isInitialized = false;
17+
18+
private static bool clearNotificationsAfterInit = false;
1419
public class NotificationDelegate : UNUserNotificationCenterDelegate
1520
{
1621
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
@@ -22,30 +27,62 @@ public override void DidReceiveNotificationResponse(UNUserNotificationCenter cen
2227
var fa = response.Notification.Request.Content.UserInfo[(NSString)"fa"];
2328
if (fa != null)
2429
{
25-
App.startingScreen = Convert.ToString(fa);
26-
HomePage.Instance().onChat(App.startingScreen, null);
27-
App.startingScreen = "";
30+
HomePage.Instance().onChat(Convert.ToString(fa), null);
2831
}
2932
}
3033
}
3134
catch (Exception ex)
3235
{
3336
Logging.error("Exception occured in DidReceiveNotificationResponse: {0}", ex);
3437
}
38+
finally
39+
{
40+
completionHandler();
41+
}
3542
}
3643
}
3744

3845
public static void initialize()
3946
{
47+
if (isInitializing
48+
|| isInitialized)
49+
{
50+
return;
51+
}
52+
53+
isInitializing = true;
4054
OneSignal.Debug.LogLevel = LogLevel.WARN;
4155
OneSignal.Debug.AlertLevel = LogLevel.NONE;
42-
56+
UNUserNotificationCenter.Current.Delegate = new NotificationDelegate();
4357
OneSignal.Initialize(SPIXI.Meta.Config.oneSignalAppId);
4458

45-
OneSignal.Notifications.RequestPermissionAsync(true);
4659

47-
OneSignal.Notifications.Clicked += handleNotificationOpened;
48-
OneSignal.Notifications.WillDisplay += handleNotificationReceived;
60+
OneSignal.Notifications.RequestPermissionAsync(true).ContinueWith(task =>
61+
{
62+
if (task.IsFaulted)
63+
{
64+
Logging.error("RequestPermissionAsync failed: {0}", task.Exception?.Flatten().InnerException?.Message);
65+
}
66+
else if (task.IsCanceled)
67+
{
68+
Logging.warn("RequestPermissionAsync was canceled.");
69+
}
70+
else
71+
{
72+
Logging.info("RequestPermissionAsync succeeded.");
73+
}
74+
75+
OneSignal.Notifications.Clicked += handleNotificationOpened;
76+
OneSignal.Notifications.WillDisplay += handleNotificationReceived;
77+
78+
isInitialized = true;
79+
80+
if (clearNotificationsAfterInit)
81+
{
82+
clearNotificationsAfterInit = false;
83+
clearNotifications();
84+
}
85+
});
4986
}
5087

5188
public static void setTag(string tag)
@@ -55,8 +92,24 @@ public static void setTag(string tag)
5592

5693
public static void clearNotifications()
5794
{
95+
if (!isInitialized)
96+
{
97+
clearNotificationsAfterInit = true;
98+
Logging.warn("Cannot clear notifications, OneSignal is not initialized yet.");
99+
return;
100+
}
101+
58102
MainThread.BeginInvokeOnMainThread(() =>
59103
{
104+
try
105+
{
106+
OneSignalNative.Notifications.ClearAll();
107+
UNUserNotificationCenter.Current.RemoveAllDeliveredNotifications();
108+
}
109+
catch (Exception e)
110+
{
111+
Logging.error("Exception while clearing all notifications: {0}.", e);
112+
}
60113
if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
61114
{
62115
// For iOS 16+, use UNUserNotificationCenter
@@ -96,7 +149,6 @@ public static void showLocalNotification(string title, string message, string da
96149

97150
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.25, false);
98151
var request = UNNotificationRequest.FromIdentifier(data, content, trigger);
99-
UNUserNotificationCenter.Current.Delegate = new NotificationDelegate();
100152

101153
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
102154
{

Spixi/Spixi.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />
224224
<PackageReference Include="CommunityToolkit.Maui" Version="9.1.1" />
225225
<PackageReference Include="Concentus" Version="2.2.2" />
226-
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.100" />
226+
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.93" />
227227
<PackageReference Include="NAudio" Version="2.2.1" Condition="$(TargetFramework.Contains('-windows'))"></PackageReference>
228228
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
229229
<PackageReference Include="OneSignalSDK.DotNet" Version="5.2.1" />
@@ -387,7 +387,7 @@
387387
</ProjectExtensions>
388388

389389
<ItemGroup>
390-
<PackageReference Update="Microsoft.Maui.Controls.Compatibility" Version="8.0.70" />
390+
<PackageReference Update="Microsoft.Maui.Controls.Compatibility" Version="8.0.93" />
391391
</ItemGroup>
392392

393393
<!-- Fix for https://github.com/dotnet/runtime/issues/96872 -->

0 commit comments

Comments
 (0)