|
30 | 30 |
|
31 | 31 | import java.lang.Integer;
|
32 | 32 | import java.util.Calendar;
|
33 |
| -import java.util.HashMap; |
34 | 33 | import java.util.Set;
|
35 | 34 | import java.util.HashSet;
|
36 | 35 | import java.util.ArrayList;
|
37 | 36 | import java.util.Arrays;
|
38 | 37 | import java.util.List;
|
| 38 | +import java.util.concurrent.ConcurrentHashMap; |
39 | 39 |
|
40 | 40 | import com.unity3d.player.UnityPlayer;
|
41 | 41 |
|
42 | 42 | public class UnityNotificationManager extends BroadcastReceiver {
|
43 | 43 | protected static NotificationCallback mNotificationCallback;
|
44 | 44 | protected static UnityNotificationManager mUnityNotificationManager;
|
45 |
| - private static HashMap<Integer, Notification> mScheduledNotifications = new HashMap(); |
| 45 | + private static ConcurrentHashMap<Integer, Object> mScheduledNotifications = new ConcurrentHashMap(); |
46 | 46 | private static HashSet<Integer> mVisibleNotifications = new HashSet<>();
|
47 | 47 |
|
48 | 48 | public Context mContext = null;
|
@@ -303,7 +303,8 @@ public NotificationChannelWrapper[] getNotificationChannels() {
|
303 | 303 | // This is called from Unity managed code to call AlarmManager to set a broadcast intent for sending a notification.
|
304 | 304 | public void scheduleNotification(Notification.Builder notificationBuilder) {
|
305 | 305 | int id = notificationBuilder.getExtras().getInt(KEY_ID, -1);
|
306 |
| - putScheduledNotification(id); |
| 306 | + // don't replace existing, only put null as placeholder so we properly report status as being scheduled |
| 307 | + mScheduledNotifications.putIfAbsent(Integer.valueOf(id), null); |
307 | 308 | mBackgroundThread.enqueueNotification(id, notificationBuilder);
|
308 | 309 | }
|
309 | 310 |
|
@@ -348,7 +349,7 @@ static Notification scheduleAlarmWithNotification(Context context, Class activit
|
348 | 349 | // fireTime not taken from notification, because we may have adjusted it
|
349 | 350 |
|
350 | 351 | Notification notification = buildNotificationForSending(context, activityClass, notificationBuilder);
|
351 |
| - putScheduledNotification(Integer.valueOf(id), notification); |
| 352 | + mScheduledNotifications.put(Integer.valueOf(id), notification); |
352 | 353 | intent.putExtra(KEY_NOTIFICATION_ID, id);
|
353 | 354 |
|
354 | 355 | PendingIntent broadcast = getBroadcastPendingIntent(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
@@ -669,7 +670,7 @@ public void onReceive(Context context, Intent intent) {
|
669 | 670 | id = builder.getExtras().getInt(KEY_NOTIFICATION_ID, -1);
|
670 | 671 | notif = buildNotificationForSending(context, openActivity, builder);
|
671 | 672 | // if notification is not sendable, it wasn't cached
|
672 |
| - putScheduledNotification(Integer.valueOf(id), notif); |
| 673 | + mScheduledNotifications.put(Integer.valueOf(id), notif); |
673 | 674 | }
|
674 | 675 |
|
675 | 676 | if (notif != null) {
|
@@ -848,7 +849,7 @@ public static Object getNotificationOrBuilderForIntent(Context context, Intent i
|
848 | 849 | if (intent.hasExtra(KEY_NOTIFICATION_ID)) {
|
849 | 850 | int id = intent.getExtras().getInt(KEY_NOTIFICATION_ID);
|
850 | 851 | Integer notificationId = Integer.valueOf(id);
|
851 |
| - if ((notification = getScheduledNotification(notificationId)) != null) { |
| 852 | + if ((notification = mScheduledNotifications.get(notificationId)) != null) { |
852 | 853 | sendable = true;
|
853 | 854 | } else {
|
854 | 855 | // in case we don't have cached notification, deserialize from storage
|
@@ -897,27 +898,16 @@ public void showNotificationSettings(String channelId) {
|
897 | 898 | mActivity.startActivity(settingsIntent);
|
898 | 899 | }
|
899 | 900 |
|
900 |
| - private static synchronized void putScheduledNotification(Integer id) { |
901 |
| - // don't replace existing, only put null as placeholder so we properly report status as being scheduled |
902 |
| - if (!mScheduledNotifications.containsKey(id)) |
903 |
| - mScheduledNotifications.put(id, null); |
904 |
| - } |
905 |
| - |
906 |
| - private static synchronized void putScheduledNotification(Integer id, Notification notification) { |
907 |
| - mScheduledNotifications.put(id, notification); |
908 |
| - } |
909 |
| - |
910 |
| - private static synchronized boolean haveScheduledNotification(Integer id) { |
911 |
| - // have key but notification is null, means scheduling is still happening on other thread |
912 |
| - return mScheduledNotifications.containsKey(id) && mScheduledNotifications.get(id) == null; |
913 |
| - } |
914 |
| - |
915 |
| - private static synchronized Notification getScheduledNotification(Integer id) { |
916 |
| - return mScheduledNotifications.get(id); |
| 901 | + private static boolean haveScheduledNotification(Integer id) { |
| 902 | + Object value = mScheduledNotifications.get(id); |
| 903 | + if (value == null) |
| 904 | + return false; |
| 905 | + // If map contains builder, in means we're still scheduling |
| 906 | + return value instanceof Notification.Builder; |
917 | 907 | }
|
918 | 908 |
|
919 |
| - protected static synchronized Notification removeScheduledNotification(Integer id) { |
920 |
| - return mScheduledNotifications.remove(id); |
| 909 | + protected static void removeScheduledNotification(Integer id) { |
| 910 | + mScheduledNotifications.remove(id); |
921 | 911 | }
|
922 | 912 | }
|
923 | 913 |
|
|
0 commit comments