Skip to content

Commit 7905040

Browse files
authored
Merge pull request #276 from Aga-C/add-multiple-messages-notification
Added multiple messages in notification
2 parents 235ac88 + 3f6acce commit 7905040

File tree

1 file changed

+36
-3
lines changed
  • app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions

1 file changed

+36
-3
lines changed

app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.media.AudioAttributes
1212
import android.media.AudioManager
1313
import android.media.RingtoneManager
1414
import android.net.Uri
15+
import android.os.Bundle
1516
import android.os.Handler
1617
import android.os.Looper
1718
import android.provider.ContactsContract.PhoneLookup
@@ -740,19 +741,18 @@ fun Context.showMessageNotification(address: String, body: String, threadId: Lon
740741
val builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL).apply {
741742
when (config.lockScreenVisibilitySetting) {
742743
LOCK_SCREEN_SENDER_MESSAGE -> {
743-
setContentTitle(sender)
744744
setLargeIcon(largeIcon)
745-
setContentText(body)
745+
setStyle(getMessagesStyle(notificationManager, threadId, sender, body))
746746
}
747747
LOCK_SCREEN_SENDER -> {
748748
setContentTitle(sender)
749749
setLargeIcon(largeIcon)
750+
setStyle(NotificationCompat.BigTextStyle().setSummaryText(summaryText).bigText(body))
750751
}
751752
}
752753

753754
color = getAdjustedPrimaryColor()
754755
setSmallIcon(R.drawable.ic_messenger)
755-
setStyle(NotificationCompat.BigTextStyle().setSummaryText(summaryText).bigText(body))
756756
setContentIntent(pendingIntent)
757757
priority = NotificationCompat.PRIORITY_MAX
758758
setDefaults(Notification.DEFAULT_LIGHTS)
@@ -771,6 +771,39 @@ fun Context.showMessageNotification(address: String, body: String, threadId: Lon
771771
notificationManager.notify(threadId.hashCode(), builder.build())
772772
}
773773

774+
private fun getMessagesStyle(notificationManager: NotificationManager, threadId: Long, sender: String, body: String): NotificationCompat.MessagingStyle {
775+
val oldMessages = getOldMessages(notificationManager, threadId)
776+
val messages = NotificationCompat.MessagingStyle(sender)
777+
oldMessages.forEach {
778+
messages.addMessage(it)
779+
}
780+
val currentMessage = NotificationCompat.MessagingStyle.Message(body, System.currentTimeMillis(), sender)
781+
messages.addMessage(currentMessage)
782+
return messages
783+
}
784+
785+
private fun getOldMessages(notificationManager: NotificationManager, threadId: Long): List<NotificationCompat.MessagingStyle.Message> {
786+
if (!isNougatPlus()) {
787+
return arrayListOf()
788+
}
789+
val currentNotification = notificationManager.activeNotifications.find { it.id == threadId.hashCode() }
790+
return if (currentNotification != null) {
791+
val messages = currentNotification.notification.extras.getParcelableArray(NotificationCompat.EXTRA_MESSAGES)
792+
val result = arrayListOf<NotificationCompat.MessagingStyle.Message>()
793+
messages?.forEach {
794+
val bundle = it as Bundle
795+
val sender = bundle.getCharSequence("sender")
796+
val text = bundle.getCharSequence("text")
797+
val time = bundle.getLong("time")
798+
val message = NotificationCompat.MessagingStyle.Message(text, time, sender)
799+
result.add(message)
800+
}
801+
return result
802+
} else {
803+
arrayListOf()
804+
}
805+
}
806+
774807
fun Context.removeDiacriticsIfNeeded(text: String): String {
775808
return if (config.useSimpleCharacters) text.normalizeString() else text
776809
}

0 commit comments

Comments
 (0)