Skip to content

Commit fefd117

Browse files
committed
Move initial housekeeping to thread class
1 parent a576076 commit fefd117

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationBackgroundThread.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.unity.androidnotifications;
22

3+
import android.content.Context;
4+
35
import java.util.concurrent.LinkedTransferQueue;
6+
import java.util.Set;
47

58
public class UnityNotificationBackgroundThread extends Thread {
69
private LinkedTransferQueue<Runnable> mTasks = new LinkedTransferQueue();
@@ -11,6 +14,8 @@ public void enqueueTask(Runnable task) {
1114

1215
@Override
1316
public void run() {
17+
performHousekeeping();
18+
1419
while (true) {
1520
try {
1621
Runnable task = mTasks.take();
@@ -24,4 +29,10 @@ public void run() {
2429

2530
android.util.Log.d("Unity", "Notification background thread exited");
2631
}
32+
33+
private void performHousekeeping() {
34+
Context context = UnityNotificationManager.mUnityNotificationManager.mContext;
35+
Set<String> notificationIds = UnityNotificationManager.getScheduledNotificationIDs(context);
36+
UnityNotificationManager.performNotificationHousekeeping(context, notificationIds);
37+
}
2738
}

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public UnityNotificationManager(Context context, Activity activity) {
7676
mContext = context;
7777
mActivity = activity;
7878
mBackgroundThread = new UnityNotificationBackgroundThread();
79-
mBackgroundThread.start();
8079

8180
try {
8281
ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
@@ -102,7 +101,7 @@ public UnityNotificationManager(Context context, Activity activity) {
102101
Log.e(TAG_UNITY, "Failed to load meta-data, NullPointer: " + e.getMessage());
103102
}
104103

105-
triggerHousekeeping(context, null);
104+
mBackgroundThread.start();
106105
}
107106

108107
public static UnityNotificationManager getNotificationManagerImpl(Context context) {
@@ -397,7 +396,7 @@ private static synchronized void triggerHousekeeping(Context context, Set<String
397396
}
398397
}
399398

400-
private static void performNotificationHousekeeping(Context context, Set<String> ids) {
399+
protected static void performNotificationHousekeeping(Context context, Set<String> ids) {
401400
Log.d(TAG_UNITY, "Checking for invalid notification IDs still hanging around");
402401

403402
Set<String> invalid = findInvalidNotificationIds(context, ids);
@@ -598,7 +597,7 @@ public void cancelAllPendingNotificationIntents() {
598597
}
599598
}
600599

601-
private static synchronized Set<String> getScheduledNotificationIDs(Context context) {
600+
protected static synchronized Set<String> getScheduledNotificationIDs(Context context) {
602601
SharedPreferences prefs = context.getSharedPreferences(NOTIFICATION_IDS_SHARED_PREFS, Context.MODE_PRIVATE);
603602
Set<String> ids = prefs.getStringSet(NOTIFICATION_IDS_SHARED_PREFS_KEY, new HashSet<String>());
604603
return ids;

0 commit comments

Comments
 (0)