Skip to content

Commit 9ddcab4

Browse files
committed
Synchronized scheduled notification caching and also removal
1 parent 141bb22 commit 9ddcab4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static Notification scheduleAlarmWithNotification(Context context, Class activit
297297
// fireTime not taken from notification, because we may have adjusted it
298298

299299
Notification notification = buildNotificationForSending(context, activityClass, notificationBuilder);
300-
mScheduledNotifications.put(Integer.valueOf(id), notification);
300+
putScheduledNotification(Integer.valueOf(id), notification);
301301
intent.putExtra(KEY_NOTIFICATION_ID, id);
302302

303303
PendingIntent broadcast = getBroadcastPendingIntent(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -404,8 +404,10 @@ private static void performNotificationHousekeeping(Context context, Set<String>
404404
synchronized (UnityNotificationManager.class) {
405405
// list might have changed while we searched
406406
Set<String> currentIds = new HashSet<>(getScheduledNotificationIDs(context));
407-
for (String id : invalid)
407+
for (String id : invalid) {
408408
currentIds.remove(id);
409+
removeScheduledNotification(Integer.valueOf(id));
410+
}
409411
saveScheduledNotificationIDs(context, currentIds);
410412
mSentSinceLastHousekeeping = 0;
411413
}
@@ -665,7 +667,7 @@ public void onReceive(Context context, Intent intent) {
665667
id = builder.getExtras().getInt(KEY_NOTIFICATION_ID, -1);
666668
notif = buildNotificationForSending(context, openActivity, builder);
667669
// if notification is not sendable, it wasn't cached
668-
mScheduledNotifications.put(Integer.valueOf(id), notif);
670+
putScheduledNotification(Integer.valueOf(id), notif);
669671
}
670672

671673
if (notif != null) {
@@ -821,8 +823,7 @@ public static Object getNotificationOrBuilderForIntent(Context context, Intent i
821823
if (intent.hasExtra(KEY_NOTIFICATION_ID)) {
822824
int id = intent.getExtras().getInt(KEY_NOTIFICATION_ID);
823825
Integer notificationId = Integer.valueOf(id);
824-
if (mScheduledNotifications.containsKey(notificationId)) {
825-
notification = mScheduledNotifications.get(notificationId);
826+
if ((notification = getScheduledNotification(notificationId)) != null) {
826827
sendable = true;
827828
} else {
828829
// in case we don't have cached notification, deserialize from storage
@@ -870,4 +871,16 @@ public void showNotificationSettings(String channelId) {
870871
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
871872
mActivity.startActivity(settingsIntent);
872873
}
874+
875+
private static synchronized void putScheduledNotification(Integer id, Notification notification) {
876+
mScheduledNotifications.put(id, notification);
877+
}
878+
879+
private static synchronized Notification getScheduledNotification(Integer id) {
880+
return mScheduledNotifications.get(id);
881+
}
882+
883+
private static synchronized Notification removeScheduledNotification(Integer id) {
884+
return mScheduledNotifications.remove(id);
885+
}
873886
}

0 commit comments

Comments
 (0)