@@ -45,6 +45,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
45
45
public Context mContext = null ;
46
46
protected Activity mActivity = null ;
47
47
protected Class mOpenActivity = null ;
48
+ protected UnityNotificationBackgroundThread mBackgroundThread ;
48
49
49
50
protected static final int SAMSUNG_NOTIFICATION_LIMIT = 500 ;
50
51
protected static final String TAG_UNITY = "UnityNotifications" ;
@@ -75,6 +76,8 @@ public UnityNotificationManager(Context context, Activity activity) {
75
76
super ();
76
77
mContext = context ;
77
78
mActivity = activity ;
79
+ mBackgroundThread = new UnityNotificationBackgroundThread ();
80
+ mBackgroundThread .start ();
78
81
79
82
try {
80
83
ApplicationInfo ai = activity .getPackageManager ().getApplicationInfo (activity .getPackageName (), PackageManager .GET_META_DATA );
@@ -385,28 +388,29 @@ private static synchronized void triggerHousekeeping(Context context, Set<String
385
388
386
389
// needed for lamda
387
390
final Set <String > notificationIds = ids ;
388
- Thread housekeepingThread = new Thread (() -> {
389
- try {
390
- // when scheduling lots of notifications at once we can have more than one housekeeping thread running
391
- // synchronize them and chain to happen one after the other
392
- synchronized (UnityNotificationManager .class ) {
393
- while (mPerformingHousekeeping ) {
394
- UnityNotificationManager .class .wait ();
391
+ if (mUnityNotificationManager != null ) {
392
+ mUnityNotificationManager .mBackgroundThread .enqueueTask (() -> {
393
+ try {
394
+ // when scheduling lots of notifications at once we can have more than one housekeeping thread running
395
+ // synchronize them and chain to happen one after the other
396
+ synchronized (UnityNotificationManager .class ) {
397
+ while (mPerformingHousekeeping ) {
398
+ UnityNotificationManager .class .wait ();
399
+ }
400
+ mPerformingHousekeeping = true ;
395
401
}
396
- mPerformingHousekeeping = true ;
397
- }
398
402
399
- performNotificationHousekeeping (context , notificationIds );
400
- } catch (InterruptedException e ) {
401
- Log .e (TAG_UNITY , "Notification housekeeping interrupted" );
402
- } finally {
403
- synchronized (UnityNotificationManager .class ) {
404
- mPerformingHousekeeping = false ;
405
- UnityNotificationManager .class .notify ();
403
+ performNotificationHousekeeping (context , notificationIds );
404
+ } catch (InterruptedException e ) {
405
+ Log .e (TAG_UNITY , "Notification housekeeping interrupted" );
406
+ } finally {
407
+ synchronized (UnityNotificationManager .class ) {
408
+ mPerformingHousekeeping = false ;
409
+ UnityNotificationManager .class .notify ();
410
+ }
406
411
}
407
- }
408
- });
409
- housekeepingThread .start ();
412
+ });
413
+ }
410
414
}
411
415
412
416
private static void performNotificationHousekeeping (Context context , Set <String > ids ) {
@@ -599,13 +603,13 @@ public void cancelAllPendingNotificationIntents() {
599
603
600
604
if (ids .size () > 0 ) {
601
605
Context context = mContext ;
602
- new Thread (() -> {
606
+ mBackgroundThread . enqueueTask (() -> {
603
607
for (String id : ids ) {
604
608
cancelPendingNotificationIntent (context , Integer .valueOf (id ));
605
609
deleteExpiredNotificationIntent (context , id );
606
610
}
607
611
triggerHousekeeping (context , null );
608
- }). start () ;
612
+ });
609
613
}
610
614
}
611
615
0 commit comments