Skip to content

Commit 6e1cd1a

Browse files
committed
Move ID checking and saving out to task
1 parent 78ec6af commit 6e1cd1a

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88

99
import java.util.concurrent.LinkedTransferQueue;
10+
import java.util.HashSet;
1011
import java.util.Set;
1112

1213
public class UnityNotificationBackgroundThread extends Thread {
@@ -21,6 +22,15 @@ public ScheduleNotificationTask(int id, Notification.Builder builder) {
2122

2223
@Override
2324
public void run() {
25+
Context context = UnityNotificationManager.mUnityNotificationManager.mContext;
26+
Set<String> ids = UnityNotificationManager.getScheduledNotificationIDs(context);
27+
String id = String.valueOf(notificationId);
28+
// are we replacing existing alarm or have capacity to schedule new one
29+
if (!(ids.contains(id) || UnityNotificationManager.canScheduleMoreAlarms(ids)))
30+
return;
31+
ids = new HashSet<>(ids);
32+
ids.add(id);
33+
UnityNotificationManager.saveScheduledNotificationIDs(context, ids);
2434
UnityNotificationManager.mUnityNotificationManager.performNotificationScheduling(notificationId, notificationBuilder);
2535
}
2636
}

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ protected void performNotificationScheduling(int id, Notification.Builder notifi
277277
fireTime += repeatInterval;
278278
}
279279

280-
Intent intent = buildNotificationIntentUpdateList(mContext, id);
280+
Intent intent = buildNotificationIntent(mContext);
281281

282282
if (intent != null) {
283283
UnityNotificationManager.saveNotification(mContext, notificationBuilder.build());
@@ -355,19 +355,6 @@ protected static Intent buildOpenAppIntent(Context context, Class className) {
355355
return openAppIntent;
356356
}
357357

358-
// Build a notification Intent to store the PendingIntent.
359-
private static synchronized Intent buildNotificationIntentUpdateList(Context context, int notificationId) {
360-
Set<String> ids = getScheduledNotificationIDs(context);
361-
if (!canScheduleMoreAlarms(ids))
362-
return null;
363-
364-
Intent intent = buildNotificationIntent(context);
365-
ids = new HashSet<>(ids);
366-
ids.add(String.valueOf(notificationId));
367-
saveScheduledNotificationIDs(context, ids);
368-
return intent;
369-
}
370-
371358
protected static boolean canScheduleMoreAlarms(Set<String> ids) {
372359
if (ids.size() < (SAMSUNG_NOTIFICATION_LIMIT - 1))
373360
return true;
@@ -574,7 +561,7 @@ protected static synchronized Set<String> getScheduledNotificationIDs(Context co
574561
return ids;
575562
}
576563

577-
private static synchronized void saveScheduledNotificationIDs(Context context, Set<String> ids) {
564+
protected static synchronized void saveScheduledNotificationIDs(Context context, Set<String> ids) {
578565
SharedPreferences.Editor editor = context.getSharedPreferences(NOTIFICATION_IDS_SHARED_PREFS, Context.MODE_PRIVATE).edit().clear();
579566
editor.putStringSet(NOTIFICATION_IDS_SHARED_PREFS_KEY, ids);
580567
editor.apply();

0 commit comments

Comments
 (0)