diff --git a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java index 656a584e9..4ac1bdaa5 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java @@ -25,8 +25,9 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.Date; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.List; +import java.util.Map; import com.wix.reactnativenotifications.core.notification.PushNotification; import com.wix.reactnativenotifications.core.NotificationIntentAdapter; @@ -44,8 +45,12 @@ public class CustomPushNotification extends PushNotification { public static final String KEY_TEXT_REPLY = "CAN_REPLY"; public static final String NOTIFICATION_REPLIED_EVENT_NAME = "notificationReplied"; - private static LinkedHashMap channelIdToNotificationCount = new LinkedHashMap(); - private static LinkedHashMap> channelIdToNotification = new LinkedHashMap>(); + private static final String PUSH_TYPE_MESSAGE = "message"; + private static final String PUSH_TYPE_CLEAR = "clear"; + private static final String PUSH_TYPE_UPDATE_BADGE = "update_badge"; + + private static Map channelIdToNotificationCount = new HashMap(); + private static Map> channelIdToNotification = new HashMap>(); private static AppLifecycleFacade lifecycleFacade; private static Context context; private static int badgeCount = 0; @@ -57,11 +62,9 @@ public class CustomPushNotification extends PushNotification { public static void clearNotification(Context mContext, int notificationId, String channelId) { if (notificationId != -1) { - Object objCount = channelIdToNotificationCount.get(channelId); - Integer count = -1; - - if (objCount != null) { - count = (Integer)objCount; + Integer count = channelIdToNotificationCount.get(channelId); + if (count == null) { + count = -1; } channelIdToNotificationCount.remove(channelId); @@ -105,22 +108,25 @@ public class CustomPushNotification extends PushNotification { if (channelId != null) { notificationId = channelId.hashCode(); - Object objCount = channelIdToNotificationCount.get(channelId); - Integer count = 1; - if (objCount != null) { - count = (Integer)objCount + 1; - } - channelIdToNotificationCount.put(channelId, count); - Object bundleArray = channelIdToNotification.get(channelId); - List list = null; - if (bundleArray == null) { - list = Collections.synchronizedList(new ArrayList(0)); - } else { - list = Collections.synchronizedList((List)bundleArray); + synchronized (channelIdToNotificationCount) { + Integer count = channelIdToNotificationCount.get(channelId); + if (count == null) { + count = 0; + } + + count += 1; + + channelIdToNotificationCount.put(channelId, count); } - synchronized (list) { - if (!"clear".equals(type)) { + + synchronized (channelIdToNotification) { + List list = channelIdToNotification.get(channelId); + if (list == null) { + list = Collections.synchronizedList(new ArrayList(0)); + } + + if (PUSH_TYPE_MESSAGE.equals(type)) { String senderName = getSenderName(data.getString("sender_name"), data.getString("channel_name"), data.getString("message")); data.putLong("time", new Date().getTime()); data.putString("sender_name", senderName); @@ -131,10 +137,13 @@ public class CustomPushNotification extends PushNotification { } } - if ("clear".equals(type)) { - cancelNotification(data, notificationId); - } else { + switch(type) { + case PUSH_TYPE_MESSAGE: super.postNotification(notificationId); + break; + case PUSH_TYPE_CLEAR: + cancelNotification(data, notificationId); + break; } notifyReceivedToJS(); @@ -382,22 +391,12 @@ public class CustomPushNotification extends PushNotification { mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext()); } - public static Integer getMessageCountInChannel(String channelId) { - Object objCount = channelIdToNotificationCount.get(channelId); - if (objCount != null) { - return (Integer)objCount; - } - - return 1; - } - private void cancelNotification(Bundle data, int notificationId) { final String channelId = data.getString("channel_id"); - final String numberString = data.getString("badge"); - - CustomPushNotification.badgeCount = Integer.parseInt(numberString); CustomPushNotification.clearNotification(mContext.getApplicationContext(), notificationId, channelId); + final String badgeString = data.getString("badge"); + CustomPushNotification.badgeCount = Integer.parseInt(badgeString); ApplicationBadgeHelper.instance.setApplicationIconBadgeNumber(mContext.getApplicationContext(), CustomPushNotification.badgeCount); } diff --git a/ios/Mattermost/AppDelegate.m b/ios/Mattermost/AppDelegate.m index 33cce9067..ff2d2f0d6 100644 --- a/ios/Mattermost/AppDelegate.m +++ b/ios/Mattermost/AppDelegate.m @@ -26,7 +26,9 @@ @implementation AppDelegate +NSString* const NotificationMessageAction = @"message"; NSString* const NotificationClearAction = @"clear"; +NSString* const NotificationUpdateBadgeAction = @"update_badge"; -(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler { os_log(OS_LOG_DEFAULT, "Mattermost will attach session from handleEventsForBackgroundURLSession!! identifier=%{public}@", identifier);