11using System ;
22using System . Collections . Generic ;
3- using System . IO ;
43using System . Linq ;
54using UnityEngine ;
65
@@ -67,9 +66,8 @@ public enum OperatingMode
6766 /// </summary>
6867 public sealed class GameNotificationsMonoBehaviour : MonoBehaviour
6968 {
70- // Default filename for notifications serializer
71- private const string _DEFAULT_FILENAME = "notifications.bin" ;
72-
69+
70+
7371 // Minimum amount of time that a notification should be into the future before it's queued when we background.
7472 private static readonly TimeSpan _minimumNotificationTime = new TimeSpan ( 0 , 0 , 2 ) ;
7573
@@ -87,23 +85,22 @@ public sealed class GameNotificationsMonoBehaviour : MonoBehaviour
8785 /// <summary>
8886 /// Event fired when a scheduled local notification is delivered while the app is in the foreground.
8987 /// </summary>
90- public Action < PendingNotification > LocalNotificationDelivered ;
88+ public Action < PendingNotification > OnLocalNotificationDelivered ;
9189
9290 /// <summary>
9391 /// Event fired when a queued local notification is cancelled because the application is in the foreground
9492 /// when it was meant to be displayed.
9593 /// </summary>
9694 /// <seealso cref="OperatingMode.Queue"/>
97- public Action < PendingNotification > LocalNotificationExpired ;
95+ public Action < PendingNotification > OnLocalNotificationExpired ;
9896
9997 private IGameNotificationsPlatform _platform ;
100- private IPendingNotificationsSerializer _serializer ;
10198 private bool _inForeground = true ;
10299
103100 /// <summary>
104101 /// Gets a collection of notifications that are scheduled or queued.
105102 /// </summary>
106- public List < PendingNotification > PendingNotifications { get ; private set ; }
103+ public List < PendingNotification > PendingNotifications { get ; private set ; } = new List < PendingNotification > ( ) ;
107104
108105 /// <summary>
109106 /// Gets whether this manager has been initialized.
@@ -147,7 +144,7 @@ private void Update()
147144 if ( time != null && time < DateTime . Now )
148145 {
149146 PendingNotifications . RemoveAt ( i ) ;
150- LocalNotificationExpired ? . Invoke ( queuedNotification ) ;
147+ OnLocalNotificationExpired ? . Invoke ( queuedNotification ) ;
151148 }
152149 }
153150 }
@@ -255,7 +252,7 @@ private void OnApplicationFocus(bool hasFocus)
255252 }
256253
257254 // Calculate notifications to save
258- var notificationsToSave = new List < PendingNotification > ( PendingNotifications . Count ) ;
255+ var notificationsToSave = new List < SerializableNotification > ( PendingNotifications . Count ) ;
259256 foreach ( var pendingNotification in PendingNotifications )
260257 {
261258 // If we're in clear mode, add nothing unless we're in rescheduling mode
@@ -273,21 +270,21 @@ private void OnApplicationFocus(bool hasFocus)
273270 pendingNotification . Notification . Scheduled &&
274271 pendingNotification . Notification . DeliveryTime . HasValue )
275272 {
276- notificationsToSave . Add ( pendingNotification ) ;
273+ notificationsToSave . Add ( pendingNotification . AsSerializableNotification ( ) ) ;
277274 }
278275 }
279276 else
280277 {
281278 // In non-clear mode, just add all scheduled notifications
282279 if ( pendingNotification . Notification . Scheduled )
283280 {
284- notificationsToSave . Add ( pendingNotification ) ;
281+ notificationsToSave . Add ( pendingNotification . AsSerializableNotification ( ) ) ;
285282 }
286283 }
287284 }
288285
289286 // Save to disk
290- _serializer . Serialize ( notificationsToSave ) ;
287+ PlayerPrefs . SetString ( "notifications" , JsonUtility . ToJson ( notificationsToSave ) ) ;
291288 }
292289
293290 /// <summary>
@@ -330,15 +327,8 @@ public void Initialize(params GameNotificationChannel[] channels)
330327 return ;
331328 }
332329
333- PendingNotifications = new List < PendingNotification > ( ) ;
334330 _platform . NotificationReceived += OnNotificationReceived ;
335331
336- // Check serializer
337- if ( _serializer == null )
338- {
339- _serializer = new DefaultSerializer ( Path . Combine ( Application . persistentDataPath , _DEFAULT_FILENAME ) ) ;
340- }
341-
342332 OnForegrounding ( ) ;
343333 }
344334
@@ -488,7 +478,7 @@ private void OnNotificationReceived(IGameNotification deliveredNotification)
488478
489479 if ( deliveredIndex >= 0 )
490480 {
491- LocalNotificationDelivered ? . Invoke ( PendingNotifications [ deliveredIndex ] ) ;
481+ OnLocalNotificationDelivered ? . Invoke ( PendingNotifications [ deliveredIndex ] ) ;
492482 PendingNotifications . RemoveAt ( deliveredIndex ) ;
493483 }
494484 }
@@ -500,7 +490,7 @@ private void OnForegrounding()
500490 _platform . OnForeground ( ) ;
501491
502492 // Deserialize saved items
503- var loaded = _serializer ? . Deserialize ( _platform ) ;
493+ var notifications = JsonUtility . FromJson < List < SerializableNotification > > ( PlayerPrefs . GetString ( "notifications" ) ) ;
504494
505495 // Foregrounding
506496 if ( ( Mode & OperatingMode . ClearOnForegrounding ) == OperatingMode . ClearOnForegrounding )
@@ -509,17 +499,18 @@ private void OnForegrounding()
509499 _platform . CancelAllScheduledNotifications ( ) ;
510500
511501 // Only reschedule in reschedule mode, and if we loaded any items
512- if ( loaded == null || ( Mode & OperatingMode . RescheduleAfterClearing ) != OperatingMode . RescheduleAfterClearing )
502+ if ( notifications == null || ( Mode & OperatingMode . RescheduleAfterClearing ) != OperatingMode . RescheduleAfterClearing )
513503 {
514504 return ;
515505 }
516506
517507 // Reschedule notifications from deserialization
518- foreach ( var savedNotification in loaded )
508+ foreach ( var savedNotification in notifications )
519509 {
520510 if ( savedNotification . DeliveryTime > DateTime . Now )
521511 {
522- var pendingNotification = ScheduleNotification ( savedNotification ) ;
512+ var pendingNotification = ScheduleNotification ( savedNotification . AsGameNotification ( _platform ) ) ;
513+
523514 pendingNotification . Reschedule = true ;
524515 }
525516 }
@@ -528,16 +519,16 @@ private void OnForegrounding()
528519 {
529520 // Just create PendingNotification wrappers for all deserialized items.
530521 // We're not rescheduling them because they were not cleared
531- if ( loaded == null )
522+ if ( notifications == null )
532523 {
533524 return ;
534525 }
535526
536- foreach ( var savedNotification in loaded )
527+ foreach ( var savedNotification in notifications )
537528 {
538529 if ( savedNotification . DeliveryTime > DateTime . Now )
539530 {
540- PendingNotifications . Add ( new PendingNotification ( savedNotification ) ) ;
531+ PendingNotifications . Add ( new PendingNotification ( savedNotification . AsGameNotification ( _platform ) ) ) ;
541532 }
542533 }
543534 }
0 commit comments