Skip to content

Commit b4689cd

Browse files
committed
FIX: Self icon (from Profile photo in Contacts) for sent messages (including direct replies).
1 parent 81cda1d commit b4689cd

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/main/java/com/oasisfeng/nevo/decorators/wechat/MessagingBuilder.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.oasisfeng.nevo.decorators.wechat;
22

3+
import android.annotation.SuppressLint;
34
import android.app.Notification;
45
import android.app.Notification.Action;
56
import android.app.NotificationManager;
@@ -32,6 +33,7 @@
3233

3334
import androidx.annotation.NonNull;
3435
import androidx.annotation.Nullable;
36+
import androidx.annotation.RequiresApi;
3537
import androidx.core.app.NotificationCompat.MessagingStyle;
3638
import androidx.core.app.NotificationCompat.MessagingStyle.Message;
3739
import androidx.core.app.Person;
@@ -44,6 +46,10 @@
4446
import static android.os.Build.VERSION_CODES.N;
4547
import static android.os.Build.VERSION_CODES.O;
4648
import static android.os.Build.VERSION_CODES.P;
49+
import static androidx.core.app.NotificationCompat.EXTRA_CONVERSATION_TITLE;
50+
import static androidx.core.app.NotificationCompat.EXTRA_IS_GROUP_CONVERSATION;
51+
import static androidx.core.app.NotificationCompat.EXTRA_MESSAGES;
52+
import static androidx.core.app.NotificationCompat.EXTRA_SELF_DISPLAY_NAME;
4753
import static com.oasisfeng.nevo.decorators.wechat.WeChatDecorator.SENDER_MESSAGE_SEPARATOR;
4854

4955
/**
@@ -77,6 +83,14 @@ class MessagingBuilder {
7783
private static final String CAR_KEY_TEXT = "text";
7884
private static final String CAR_KEY_TIMESTAMP = "timestamp";
7985

86+
private static final String KEY_TEXT = "text";
87+
private static final String KEY_TIMESTAMP = "time";
88+
private static final String KEY_SENDER = "sender";
89+
@RequiresApi(P) private static final String KEY_SENDER_PERSON = "sender_person";
90+
private static final String KEY_DATA_MIME_TYPE = "type";
91+
private static final String KEY_DATA_URI= "uri";
92+
private static final String KEY_EXTRAS_BUNDLE = "extras";
93+
8094
private static final String KEY_USERNAME = "key_username";
8195
private static final String MENTION_SEPARATOR = " "; // Separator between @nick and text. It's not a regular white space, but U+2005.
8296

@@ -335,6 +349,46 @@ void markRead(final String key) {
335349
return new Intent().addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES).setPackage(action.getCreatorPackage());
336350
}
337351

352+
static void flatIntoExtras(final MessagingStyle messaging, final Bundle extras) {
353+
final Person user = messaging.getUser();
354+
if (user != null) {
355+
extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, user.getName());
356+
if (SDK_INT >= P) extras.putParcelable(Notification.EXTRA_MESSAGING_PERSON, toAndroidPerson(user)); // Not included in NotificationCompat
357+
}
358+
if (messaging.getConversationTitle() != null) extras.putCharSequence(EXTRA_CONVERSATION_TITLE, messaging.getConversationTitle());
359+
final List<Message> messages = messaging.getMessages();
360+
if (! messages.isEmpty()) extras.putParcelableArray(EXTRA_MESSAGES, getBundleArrayForMessages(messages));
361+
//if (! mHistoricMessages.isEmpty()) extras.putParcelableArray(Notification.EXTRA_HISTORIC_MESSAGES, MessagingBuilder.getBundleArrayForMessages(mHistoricMessages));
362+
extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, messaging.isGroupConversation());
363+
}
364+
365+
private static Bundle[] getBundleArrayForMessages(final List<Message> messages) {
366+
final int N = messages.size();
367+
final Bundle[] bundles = new Bundle[N];
368+
for (int i = 0; i < N; i ++) bundles[i] = toBundle(messages.get(i));
369+
return bundles;
370+
}
371+
372+
private static Bundle toBundle(final Message message) {
373+
final Bundle bundle = new Bundle();
374+
bundle.putCharSequence(KEY_TEXT, message.getText());
375+
bundle.putLong(KEY_TIMESTAMP, message.getTimestamp()); // Must be included even for 0
376+
final Person sender = message.getPerson();
377+
if (sender != null) {
378+
bundle.putCharSequence(KEY_SENDER, sender.getName()); // Legacy listeners need this
379+
if (SDK_INT >= P) bundle.putParcelable(KEY_SENDER_PERSON, toAndroidPerson(sender));
380+
}
381+
if (message.getDataMimeType() != null) bundle.putString(KEY_DATA_MIME_TYPE, message.getDataMimeType());
382+
if (message.getDataUri() != null) bundle.putParcelable(KEY_DATA_URI, message.getDataUri());
383+
if (SDK_INT >= O && ! message.getExtras().isEmpty()) bundle.putBundle(KEY_EXTRAS_BUNDLE, message.getExtras());
384+
//if (message.isRemoteInputHistory()) bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, message.isRemoteInputHistory());
385+
return bundle;
386+
}
387+
388+
@RequiresApi(P) @SuppressLint("RestrictedApi") private static android.app.Person toAndroidPerson(final Person user) {
389+
return user.toAndroidPerson();
390+
}
391+
338392
interface Controller { void recastNotification(String key, Bundle addition); }
339393

340394
MessagingBuilder(final Context context, final Controller controller) {

src/main/java/com/oasisfeng/nevo/decorators/wechat/WeChatDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public class WeChatDecorator extends NevoDecoratorService {
140140
if (messages.isEmpty()) return;
141141

142142
if (is_group_chat) messaging.setGroupConversation(true).setConversationTitle(title);
143-
messaging.addCompatExtras(extras);
143+
MessagingBuilder.flatIntoExtras(messaging, extras);
144144
extras.putString(Notification.EXTRA_TEMPLATE, TEMPLATE_MESSAGING);
145145

146146
if (SDK_INT >= N && extras.getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY) != null)

0 commit comments

Comments
 (0)