66using SPIXI;
77using UIKit;
88using UserNotifications;
9+ using OneSignalNative = Com.OneSignal.iOS.OneSignal;
910
1011namespace 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 {
0 commit comments