1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
3
using System . Linq ;
5
4
using UnityEngine ;
6
5
@@ -67,9 +66,8 @@ public enum OperatingMode
67
66
/// </summary>
68
67
public sealed class GameNotificationsMonoBehaviour : MonoBehaviour
69
68
{
70
- // Default filename for notifications serializer
71
- private const string _DEFAULT_FILENAME = "notifications.bin" ;
72
-
69
+
70
+
73
71
// Minimum amount of time that a notification should be into the future before it's queued when we background.
74
72
private static readonly TimeSpan _minimumNotificationTime = new TimeSpan ( 0 , 0 , 2 ) ;
75
73
@@ -87,23 +85,22 @@ public sealed class GameNotificationsMonoBehaviour : MonoBehaviour
87
85
/// <summary>
88
86
/// Event fired when a scheduled local notification is delivered while the app is in the foreground.
89
87
/// </summary>
90
- public Action < PendingNotification > LocalNotificationDelivered ;
88
+ public Action < PendingNotification > OnLocalNotificationDelivered ;
91
89
92
90
/// <summary>
93
91
/// Event fired when a queued local notification is cancelled because the application is in the foreground
94
92
/// when it was meant to be displayed.
95
93
/// </summary>
96
94
/// <seealso cref="OperatingMode.Queue"/>
97
- public Action < PendingNotification > LocalNotificationExpired ;
95
+ public Action < PendingNotification > OnLocalNotificationExpired ;
98
96
99
97
private IGameNotificationsPlatform _platform ;
100
- private IPendingNotificationsSerializer _serializer ;
101
98
private bool _inForeground = true ;
102
99
103
100
/// <summary>
104
101
/// Gets a collection of notifications that are scheduled or queued.
105
102
/// </summary>
106
- public List < PendingNotification > PendingNotifications { get ; private set ; }
103
+ public List < PendingNotification > PendingNotifications { get ; private set ; } = new List < PendingNotification > ( ) ;
107
104
108
105
/// <summary>
109
106
/// Gets whether this manager has been initialized.
@@ -147,7 +144,7 @@ private void Update()
147
144
if ( time != null && time < DateTime . Now )
148
145
{
149
146
PendingNotifications . RemoveAt ( i ) ;
150
- LocalNotificationExpired ? . Invoke ( queuedNotification ) ;
147
+ OnLocalNotificationExpired ? . Invoke ( queuedNotification ) ;
151
148
}
152
149
}
153
150
}
@@ -255,7 +252,7 @@ private void OnApplicationFocus(bool hasFocus)
255
252
}
256
253
257
254
// Calculate notifications to save
258
- var notificationsToSave = new List < PendingNotification > ( PendingNotifications . Count ) ;
255
+ var notificationsToSave = new List < SerializableNotification > ( PendingNotifications . Count ) ;
259
256
foreach ( var pendingNotification in PendingNotifications )
260
257
{
261
258
// If we're in clear mode, add nothing unless we're in rescheduling mode
@@ -273,21 +270,21 @@ private void OnApplicationFocus(bool hasFocus)
273
270
pendingNotification . Notification . Scheduled &&
274
271
pendingNotification . Notification . DeliveryTime . HasValue )
275
272
{
276
- notificationsToSave . Add ( pendingNotification ) ;
273
+ notificationsToSave . Add ( pendingNotification . AsSerializableNotification ( ) ) ;
277
274
}
278
275
}
279
276
else
280
277
{
281
278
// In non-clear mode, just add all scheduled notifications
282
279
if ( pendingNotification . Notification . Scheduled )
283
280
{
284
- notificationsToSave . Add ( pendingNotification ) ;
281
+ notificationsToSave . Add ( pendingNotification . AsSerializableNotification ( ) ) ;
285
282
}
286
283
}
287
284
}
288
285
289
286
// Save to disk
290
- _serializer . Serialize ( notificationsToSave ) ;
287
+ PlayerPrefs . SetString ( "notifications" , JsonUtility . ToJson ( notificationsToSave ) ) ;
291
288
}
292
289
293
290
/// <summary>
@@ -330,15 +327,8 @@ public void Initialize(params GameNotificationChannel[] channels)
330
327
return ;
331
328
}
332
329
333
- PendingNotifications = new List < PendingNotification > ( ) ;
334
330
_platform . NotificationReceived += OnNotificationReceived ;
335
331
336
- // Check serializer
337
- if ( _serializer == null )
338
- {
339
- _serializer = new DefaultSerializer ( Path . Combine ( Application . persistentDataPath , _DEFAULT_FILENAME ) ) ;
340
- }
341
-
342
332
OnForegrounding ( ) ;
343
333
}
344
334
@@ -488,7 +478,7 @@ private void OnNotificationReceived(IGameNotification deliveredNotification)
488
478
489
479
if ( deliveredIndex >= 0 )
490
480
{
491
- LocalNotificationDelivered ? . Invoke ( PendingNotifications [ deliveredIndex ] ) ;
481
+ OnLocalNotificationDelivered ? . Invoke ( PendingNotifications [ deliveredIndex ] ) ;
492
482
PendingNotifications . RemoveAt ( deliveredIndex ) ;
493
483
}
494
484
}
@@ -500,7 +490,7 @@ private void OnForegrounding()
500
490
_platform . OnForeground ( ) ;
501
491
502
492
// Deserialize saved items
503
- var loaded = _serializer ? . Deserialize ( _platform ) ;
493
+ var notifications = JsonUtility . FromJson < List < SerializableNotification > > ( PlayerPrefs . GetString ( "notifications" ) ) ;
504
494
505
495
// Foregrounding
506
496
if ( ( Mode & OperatingMode . ClearOnForegrounding ) == OperatingMode . ClearOnForegrounding )
@@ -509,17 +499,18 @@ private void OnForegrounding()
509
499
_platform . CancelAllScheduledNotifications ( ) ;
510
500
511
501
// 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 )
513
503
{
514
504
return ;
515
505
}
516
506
517
507
// Reschedule notifications from deserialization
518
- foreach ( var savedNotification in loaded )
508
+ foreach ( var savedNotification in notifications )
519
509
{
520
510
if ( savedNotification . DeliveryTime > DateTime . Now )
521
511
{
522
- var pendingNotification = ScheduleNotification ( savedNotification ) ;
512
+ var pendingNotification = ScheduleNotification ( savedNotification . AsGameNotification ( _platform ) ) ;
513
+
523
514
pendingNotification . Reschedule = true ;
524
515
}
525
516
}
@@ -528,16 +519,16 @@ private void OnForegrounding()
528
519
{
529
520
// Just create PendingNotification wrappers for all deserialized items.
530
521
// We're not rescheduling them because they were not cleared
531
- if ( loaded == null )
522
+ if ( notifications == null )
532
523
{
533
524
return ;
534
525
}
535
526
536
- foreach ( var savedNotification in loaded )
527
+ foreach ( var savedNotification in notifications )
537
528
{
538
529
if ( savedNotification . DeliveryTime > DateTime . Now )
539
530
{
540
- PendingNotifications . Add ( new PendingNotification ( savedNotification ) ) ;
531
+ PendingNotifications . Add ( new PendingNotification ( savedNotification . AsGameNotification ( _platform ) ) ) ;
541
532
}
542
533
}
543
534
}
0 commit comments