diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index e79b72f0e904..2ad4e0add0a9 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,79 +1,92 @@ + xmlns:tools="http://schemas.android.com/tools" + package="com.expensify.chat"> - + - - + + - - + android:resizeableActivity="false" + android:roundIcon="@mipmap/ic_launcher_round" + android:supportsRtl="false" + android:theme="@style/AppTheme" + tools:replace="android:supportsRtl"> + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + - - - - - - - + + + + + + + + + + + + - - - - - + - - - - - - - - - - + - - - - - - - - - - - - + - diff --git a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java index eff3420ee96d..d7dff0ffcf0f 100644 --- a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java +++ b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java @@ -43,6 +43,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -61,15 +62,16 @@ public class CustomNotificationProvider extends ReactNotificationProvider { // Define notification channel public static final String CHANNEL_MESSAGES_ID = "CHANNEL_MESSAGES"; - public static final String CHANNEL_MESSAGES_NAME = "Message Notifications"; - public static final String CHANNEL_GROUP_ID = "CHANNEL_GROUP_CHATS"; + public static final String CHANNEL_MESSAGES_NAME = "Messages"; + public static final String NOTIFICATION_GROUP_CHATS = "NOTIFICATION_GROUP_CHATS"; public static final String CHANNEL_GROUP_NAME = "Chats"; // Conversation JSON keys private static final String PAYLOAD_KEY = "payload"; + private static final String ONYX_DATA_KEY = "onyxData"; private final ExecutorService executorService = Executors.newCachedThreadPool(); - public final HashMap cache = new HashMap<>(); + public final HashMap cache = new HashMap<>(); public CustomNotificationProvider(@NonNull Context context, @NonNull AirshipConfigOptions configOptions) { super(context, configOptions); @@ -83,6 +85,7 @@ public CustomNotificationProvider(@NonNull Context context, @NonNull AirshipConf protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @NonNull NotificationCompat.Builder builder, @NonNull NotificationArguments arguments) { super.onExtendBuilder(context, builder, arguments); PushMessage message = arguments.getMessage(); + Log.d(TAG, "buildNotification: " + message.toString()); // Improve notification delivery by categorising as a time-critical message builder.setCategory(CATEGORY_MESSAGE); @@ -98,10 +101,10 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @ if (message.containsKey(PAYLOAD_KEY)) { try { JsonMap payload = JsonValue.parseString(message.getExtra(PAYLOAD_KEY)).optMap(); - - // Apply message style using onyxData from the notification payload - if (payload.get("onyxData").getList().size() > 0) { - applyMessageStyle(context, builder, payload, arguments.getNotificationId()); + if (payload.containsKey(ONYX_DATA_KEY)) { + Objects.requireNonNull(payload.get(ONYX_DATA_KEY)).isNull(); + Log.d(TAG, "payload contains onxyData"); + applyMessageStyle(context, builder, payload, arguments.getNotificationId()); } } catch (Exception e) { Log.e(TAG, "Failed to parse conversation, falling back to default notification style. SendID=" + message.getSendId(), e); @@ -113,9 +116,8 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @ @RequiresApi(api = Build.VERSION_CODES.O) private void createAndRegisterNotificationChannel(@NonNull Context context) { - NotificationChannelGroup channelGroup = new NotificationChannelGroup(CHANNEL_GROUP_ID, CHANNEL_GROUP_NAME); + NotificationChannelGroup channelGroup = new NotificationChannelGroup(NOTIFICATION_GROUP_CHATS, CHANNEL_GROUP_NAME); NotificationChannel channel = new NotificationChannel(CHANNEL_MESSAGES_ID, CHANNEL_MESSAGES_NAME, NotificationManager.IMPORTANCE_HIGH); - channel.setGroup(CHANNEL_GROUP_ID); NotificationManager notificationManager = context.getSystemService(NotificationManager.class); notificationManager.createNotificationChannelGroup(channelGroup); @@ -160,15 +162,17 @@ public Bitmap getCroppedBitmap(Bitmap bitmap) { * @param notificationID Current notification ID */ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Builder builder, JsonMap payload, int notificationID) { - int reportID = payload.get("reportID").getInt(-1); + long reportID = payload.get("reportID").getLong(-1); if (reportID == -1) { return; } + // Retrieve and check for cached notifications NotificationCache notificationCache = findOrCreateNotificationCache(reportID); + boolean hasExistingNotification = notificationCache.messages.size() >= 1; try { - JsonMap reportMap = payload.get("onyxData").getList().get(1).getMap().get("value").getMap(); + JsonMap reportMap = payload.get(ONYX_DATA_KEY).getList().get(1).getMap().get("value").getMap(); String reportId = reportMap.keySet().iterator().next(); JsonMap messageData = reportMap.get(reportId).getMap(); @@ -176,14 +180,20 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil String avatar = messageData.get("avatar").getString(); String accountID = Integer.toString(messageData.get("actorAccountID").getInt(-1)); String message = messageData.get("message").getList().get(0).getMap().get("text").getString(); - - String roomName = payload.get("roomName") == null ? "" : payload.get("roomName").getString(""); - String conversationTitle = roomName.isEmpty() ? "Chat with " + name : roomName; + String conversationName = payload.get("roomName") == null ? "" : payload.get("roomName").getString(""); // Retrieve or create the Person object who sent the latest report comment Person person = notificationCache.people.get(accountID); + Bitmap personIcon = notificationCache.bitmapIcons.get(accountID); + + if (personIcon == null) { + personIcon = fetchIcon(context, avatar); + } + builder.setLargeIcon(personIcon); + + // Persist the person and icon to the notification cache if (person == null) { - IconCompat iconCompat = fetchIcon(context, avatar); + IconCompat iconCompat = IconCompat.createWithBitmap(personIcon); person = new Person.Builder() .setIcon(iconCompat) .setKey(accountID) @@ -191,21 +201,26 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil .build(); notificationCache.people.put(accountID, person); + notificationCache.bitmapIcons.put(accountID, personIcon); } - // Store the latest report comment in the local conversation history + // Despite not using conversation style for the initial notification from each chat, we need to cache it to enable conversation style for future notifications long createdTimeInMillis = getMessageTimeInMillis(messageData.get("created").getString("")); notificationCache.messages.add(new NotificationCache.Message(person, message, createdTimeInMillis)); - // Create the messaging style notification builder for this notification, associating the - // notification with the person who sent the report comment. - NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(person) - .setGroupConversation(notificationCache.people.size() > 2 || !roomName.isEmpty()) - .setConversationTitle(conversationTitle); - // Add all conversation messages to the notification, including the last one we just received. - for (NotificationCache.Message cachedMessage : notificationCache.messages) { - messagingStyle.addMessage(cachedMessage.text, cachedMessage.time, cachedMessage.person); + // Conversational styling should be applied to groups chats, rooms, and any 1:1 chats with more than one notification (ensuring the large profile image is always shown) + if (!conversationName.isEmpty() || hasExistingNotification) { + // Create the messaging style notification builder for this notification, associating it with the person who sent the report comment + NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(person) + .setGroupConversation(true) + .setConversationTitle(conversationName); + + // Add all conversation messages to the notification, including the last one we just received. + for (NotificationCache.Message cachedMessage : notificationCache.messages) { + messagingStyle.addMessage(cachedMessage.text, cachedMessage.time, cachedMessage.person); + } + builder.setStyle(messagingStyle); } // Clear the previous notification associated to this conversation so it looks like we are @@ -214,9 +229,6 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil NotificationManagerCompat.from(context).cancel(notificationCache.prevNotificationID); } - // Apply the messaging style to the notification builder - builder.setStyle(messagingStyle); - } catch (Exception e) { e.printStackTrace(); } @@ -250,7 +262,7 @@ private long getMessageTimeInMillis(String createdTime) { * @param reportID Report ID. * @return Notification Cache. */ - private NotificationCache findOrCreateNotificationCache(int reportID) { + private NotificationCache findOrCreateNotificationCache(long reportID) { NotificationCache notificationCache = cache.get(reportID); if (notificationCache == null) { @@ -269,7 +281,7 @@ private NotificationCache findOrCreateNotificationCache(int reportID) { public void onDismissNotification(PushMessage message) { try { JsonMap payload = JsonValue.parseString(message.getExtra(PAYLOAD_KEY)).optMap(); - int reportID = payload.get("reportID").getInt(-1); + long reportID = payload.get("reportID").getLong(-1); if (reportID == -1) { return; @@ -281,7 +293,7 @@ public void onDismissNotification(PushMessage message) { } } - private IconCompat fetchIcon(@NonNull Context context, String urlString) { + private Bitmap fetchIcon(@NonNull Context context, String urlString) { URL parsedUrl = null; try { parsedUrl = urlString == null ? null : new URL(urlString); @@ -305,7 +317,7 @@ private IconCompat fetchIcon(@NonNull Context context, String urlString) { try { Bitmap bitmap = future.get(MAX_ICON_FETCH_WAIT_TIME_SECONDS, TimeUnit.SECONDS); - return IconCompat.createWithBitmap(getCroppedBitmap(bitmap)); + return getCroppedBitmap(bitmap); } catch (InterruptedException e) { Log.e(TAG,"Failed to fetch icon", e); Thread.currentThread().interrupt(); @@ -320,6 +332,8 @@ private IconCompat fetchIcon(@NonNull Context context, String urlString) { private static class NotificationCache { public Map people = new HashMap<>(); public ArrayList messages = new ArrayList<>(); + + public Map bitmapIcons = new HashMap<>(); public int prevNotificationID = -1; public static class Message { diff --git a/android/app/src/main/res/drawable/ic_launcher.png b/android/app/src/main/res/drawable/ic_launcher.png new file mode 100644 index 000000000000..1e5a088c3e8b Binary files /dev/null and b/android/app/src/main/res/drawable/ic_launcher.png differ