diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 013facf04..d627a78f8 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -42,8 +42,7 @@ - - + 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 b56ef87b2..1812ef689 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java @@ -1,6 +1,7 @@ package com.mattermost.rnbeta; import android.app.PendingIntent; +import android.content.Intent; import android.content.Context; import android.content.res.Resources; import android.content.pm.ApplicationInfo; @@ -11,6 +12,7 @@ import android.os.Build; import android.app.Notification; import android.app.NotificationManager; import java.util.LinkedHashMap; +import java.util.ArrayList; import com.wix.reactnativenotifications.core.notification.PushNotification; import com.wix.reactnativenotifications.core.AppLaunchHelper; @@ -24,12 +26,22 @@ public class CustomPushNotification extends PushNotification { public static final int MESSAGE_NOTIFICATION_ID = 435345; public static final String GROUP_KEY_MESSAGES = "mm_group_key_messages"; + public static final String NOTIFICATION_ID = "notificationId"; private static LinkedHashMap channelIdToNotificationCount = new LinkedHashMap(); + private static LinkedHashMap> channelIdToNotification = new LinkedHashMap>(); public CustomPushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) { super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper); } + public static void clearNotification(int notificationId) { + if (notificationId != -1) { + String channelId = String.valueOf(notificationId); + channelIdToNotificationCount.remove(channelId); + channelIdToNotification.remove(channelId); + } + } + @Override public void onReceived() throws InvalidNotificationException { Bundle data = mNotificationProps.asBundle(); @@ -44,6 +56,16 @@ public class CustomPushNotification extends PushNotification { count = (Integer)objCount + 1; } channelIdToNotificationCount.put(channelId, count); + + Object bundleArray = channelIdToNotification.get(channelId); + ArrayList list = null; + if (bundleArray == null) { + list = new ArrayList(); + } else { + list = (ArrayList)bundleArray; + } + list.add(data); + channelIdToNotification.put(channelId, list); } if ("clear".equals(type)) { @@ -55,6 +77,16 @@ public class CustomPushNotification extends PushNotification { notifyReceivedToJS(); } + @Override + public void onOpened() { + Bundle data = mNotificationProps.asBundle(); + final String channelId = data.getString("channel_id"); + channelIdToNotificationCount.remove(channelId); + channelIdToNotification.remove(channelId); + digestNotification(); + clearAllNotifications(); + } + @Override protected void postNotification(int id, Notification notification) { if (!mAppLifecycleFacade.isAppVisible()) { @@ -118,19 +150,40 @@ public class CustomPushNotification extends PushNotification { } notification - .setContentTitle(title) - .setContentText(message) .setGroupSummary(true) .setSmallIcon(smallIconResId) .setVisibility(Notification.VISIBILITY_PRIVATE) .setPriority(Notification.PRIORITY_HIGH); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - notification.setGroup(GROUP_KEY_MESSAGES); + if (numMessages == 1) { + notification + .setContentTitle(title) + .setContentText(message) + .setStyle(new Notification.BigTextStyle() + .bigText(message)); + } else { + String summaryTitle = String.format("%s (%d)", title, numMessages); + + Notification.InboxStyle style = new Notification.InboxStyle(); + ArrayList list = (ArrayList) channelIdToNotification.get(channelId); + for (Bundle data : list){ + style.addLine(data.getString("message")); + } + + style.setBigContentTitle(title); + notification.setStyle(style) + .setContentTitle(summaryTitle); +// .setNumber(numMessages); } - if (numMessages > 1) { - notification.setNumber(numMessages); + // Let's add a delete intent when the notification is dismissed + Intent delIntent = new Intent(mContext, NotificationDismissReceiver.class); + delIntent.putExtra(NOTIFICATION_ID, notificationId); + PendingIntent deleteIntent = PendingIntent.getBroadcast(mContext, 0, delIntent, 0); + notification.setDeleteIntent(deleteIntent); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + notification.setGroup(GROUP_KEY_MESSAGES); } Bitmap largeIconBitmap = BitmapFactory.decodeResource(res, largeIconResId); @@ -158,6 +211,7 @@ public class CustomPushNotification extends PushNotification { } channelIdToNotificationCount.remove(channelId); + channelIdToNotification.remove(channelId); final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(notificationId); } diff --git a/android/app/src/main/java/com/mattermost/rnbeta/NotificationDismissReceiver.java b/android/app/src/main/java/com/mattermost/rnbeta/NotificationDismissReceiver.java new file mode 100644 index 000000000..be0706b39 --- /dev/null +++ b/android/app/src/main/java/com/mattermost/rnbeta/NotificationDismissReceiver.java @@ -0,0 +1,14 @@ +package com.mattermost.rnbeta; + +import android.content.Context; +import android.content.Intent; +import android.content.BroadcastReceiver; + + +public class NotificationDismissReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + int notificationId = intent.getIntExtra(CustomPushNotification.NOTIFICATION_ID, -1); + CustomPushNotification.clearNotification(notificationId); + } +}