So I'm building a messaging app and I'm trying to use NotificationCompat.MessagingStyle for my notifications it works generally for one message but each message added just replaces the last message, rather than show each and every message received until there cleared or viewed (desired result), having a look at the docs they say to add a message like this
.addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
.addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
which leads me to assume they are storing these messages in an array somewhere but I'm not sure where to implement this as if i create a new array in the same class I assume it will be the same result should it be saved to shared preferences (seems a bit memory heavy for that) or perhaps in a small database of unread messages? I already store a value in my messages for whether they have been sent or received I could run a query for all unread messages populate a list and iterate over it although this seems like overkill any ideas?
UPDATE
still not getting anywhere with this, tried to make a list of messages and adding them like this but still no joy
NotificationCompat.MessagingStyle messagingStyle = new
NotificationCompat.MessagingStyle(message.getRecipientName());
messagingStyle.setConversationTitle(
getSmsTodayYestFromMilli(message.getTime_stamp().getTime()));
if (messages != null)
for(DatabaseMessage dbMessage : messages){
messagingStyle.addMessage(dbMessage.getMessage(),
dbMessage.getTime_stamp().getTime(),dbMessage.getRecipientName());
}
}
notificationBuilder.setStyle(messagingStyle);
I know this is an old question, but this can be useful for some.
You can extract the previous messages from the existing notification:
NotificationCompat.MessagingStyle style = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(existingNotification);
style.getMessages();
So I found out how to do it but still not sure on where to store the list but to add a message you actually need to create a NotificationCompat.MessagingStyle.Message
and then use the addMessage
method, right now I'm trying to get a list of all the messages that are unread and iterate over them, but its still having problems but I think the problems warrant a new question here's the adding a message:
NotificationCompat.MessagingStyle messagingStyle = new
NotificationCompat.MessagingStyle(message.getRecipientName());
messagingStyle.setConversationTitle(getSmsTodayYestFromMilli(
message.getTime_stamp().getTime()));
if (messages != null){
Log.d(MYTAG,"list size " + messages.size());
for(DatabaseMessage databaseMessage : messages){
NotificationCompat.MessagingStyle.Message notificationMessage = new
NotificationCompat.MessagingStyle.Message(
databaseMessage.getMessage(),
databaseMessage.getTime_stamp().getTime(),
databaseMessage.getRecipientName()
);
messagingStyle.addMessage(notificationMessage);
}
}
notificationBuilder.setStyle(messagingStyle);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With