Skip to content

Commit eace63a

Browse files
committed
Prevent race condition if status is queried immediately after scheduling
1 parent c9169bc commit eace63a

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ public Object[] getNotificationChannels() {
261261

262262
// This is called from Unity managed code to call AlarmManager to set a broadcast intent for sending a notification.
263263
public void scheduleNotification(Notification.Builder notificationBuilder) {
264+
int id = notificationBuilder.getExtras().getInt(KEY_ID, -1);
265+
putScheduledNotification(id);
264266
mBackgroundThread.enqueueTask(() -> {
265267
Bundle extras = notificationBuilder.getExtras();
266-
int id = extras.getInt(KEY_ID, -1);
267268
long repeatInterval = extras.getLong(KEY_REPEAT_INTERVAL, -1);
268269
long fireTime = extras.getLong(KEY_FIRE_TIME, -1);
269270
Notification notification = null;
@@ -583,6 +584,8 @@ public int checkNotificationStatus(int id) {
583584
}
584585
}
585586

587+
if (haveScheduledNotification(id))
588+
return 1;
586589
if (checkIfPendingNotificationIsRegistered(id))
587590
return 1;
588591

@@ -912,10 +915,21 @@ public void showNotificationSettings(String channelId) {
912915
mActivity.startActivity(settingsIntent);
913916
}
914917

918+
private static synchronized void putScheduledNotification(Integer id) {
919+
// don't replace existing, only put null as placeholder so we properly report status as being scheduled
920+
if (!mScheduledNotifications.containsKey(id))
921+
mScheduledNotifications.put(id, null);
922+
}
923+
915924
private static synchronized void putScheduledNotification(Integer id, Notification notification) {
916925
mScheduledNotifications.put(id, notification);
917926
}
918927

928+
private static synchronized boolean haveScheduledNotification(Integer id) {
929+
// have key but notification is null, means scheduling is still happening on other thread
930+
return mScheduledNotifications.containsKey(id) && mScheduledNotifications.get(id) == null;
931+
}
932+
919933
private static synchronized Notification getScheduledNotification(Integer id) {
920934
return mScheduledNotifications.get(id);
921935
}

0 commit comments

Comments
 (0)