Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse existing messages when using MessagingStyle #1781

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions src/android/notification/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import android.graphics.RectF;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.app.NotificationManager;
import android.service.notification.StatusBarNotification;
import android.os.Build;

import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -261,7 +264,7 @@ private void applyStyle(NotificationCompat.Builder builder) {
}

/**
* Apply inbox style.
* Apply messaging style.
*
* @param builder Local notification builder instance.
* @param messages The messages to add to the conversation.
Expand All @@ -270,14 +273,43 @@ private void applyMessagingStyle(NotificationCompat.Builder builder,
Message[] messages) {

NotificationCompat.MessagingStyle style;
String title = options.getTitle();

style = new NotificationCompat.MessagingStyle("Me")
.setConversationTitle(options.getTitle());
// Find if there is a notification already displayed with this ID.
android.app.Notification notification = findActiveNotification(options.getId());

if (notification != null) {
// Notification already displayed. Extract the MessagingStyle to add the message.
style = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(notification);
} else {
// There is no notification, create a new style.
style = new NotificationCompat.MessagingStyle("");
}

// Add the new messages to the style.
for (Message msg : messages) {
style.addMessage(msg);
}

// Add the count of messages to the title if there is more than 1.
Integer sizeList = style.getMessages().size();

if (sizeList > 1) {
String count = "(" + sizeList + ")"; // Default value.

if (options.getTitleCount() != null) {
count = options.getTitleCount();
count = count.replace("%n%", "" + sizeList);
}

if (!count.trim().equals("")) {
title += " " + count;
}
}

style.setConversationTitle(title);

// Use the style.
builder.setStyle(style);
}

Expand Down Expand Up @@ -479,4 +511,27 @@ private NotificationCompat.Builder findOrCreateBuilder() {
return builder;
}

/**
* Find an active notification with a certain ID.
*
* @param notId Notification ID to find.
* @return Notification The active notification, null if not found.
*/
private android.app.Notification findActiveNotification(Integer notId) {
// The getActiveNotifications method is only available from Android M.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();

// Find the notification.
for (int i = 0; i < notifications.length; i++) {
if (notifications[i].getId() == notId) {
return notifications[i].getNotification();
}
}
}

return null;
}

}
13 changes: 12 additions & 1 deletion src/android/notification/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,18 @@ public void show() {