Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessagingStyle Not Displaying Text

New to Android development and I’m trying out the latest addHistoricMessage, and I’m missing something because it’s not displaying anything. On rare occasion the addMessage text is displayed, but the addHistoricMessage is never displayed. addMessage works consistently when using NotificationCompat, but NotificationCompat doesn’t appear to have addHistoricMessage.

Any thoughts appreciated - using androidx.appcompat:appcompat:1.0.2 and compileSdkVersion and targetSdkVersion are both 28.

An example of what I’m seeing is:

Notification without MessagingStyle messages

Test button that calls notification:

fun test(view: View) {
    val job = GlobalScope.launch {
        val repository = DataRepository.getInstance(Db.getDb(this@MainActivity))

        AlarmReceiver().notifyTest(
                this@MainActivity,
                repository.upcomingDetail(9),
                arrayListOf("Hi!", "Miss you!", "Hello!")
        )
    }
}

Notification methods and related (less important code removed):

fun notifyTest(context: Context, upcoming: UpcomingDetail, top3Sent: List<String>?) {
    //...

    @TargetApi(Build.VERSION_CODES.P)
    when (Build.VERSION.SDK_INT) {
        in 1..27 -> {
            with(NotificationManagerCompat.from(context)) {
                notify(upcoming.id.toInt(), legacyNotificationBuilder(
                        context,
                        upcoming,
                        noteIntent,
                        contentPending,
                        disablePending,
                        deletePending,
                        postponePending,
                        top3Sent
                ).build())
            }
        }
        else -> context.getSystemService(NotificationManager::class.java)
                .notify(upcoming.id.toInt(), notificationBuilder(
                        context,
                        upcoming,
                        noteIntent,
                        contentPending,
                        disablePending,
                        deletePending,
                        postponePending,
                        top3Sent
                ).build())
    }
}

@RequiresApi(Build.VERSION_CODES.P)
private fun notificationBuilder(
        context: Context,
        upcoming: UpcomingDetail,
        noteIntent: Intent,
        contentPending: PendingIntent,
        deletePending: PendingIntent,
        disablePending: PendingIntent,
        postponePending: PendingIntent,
        top3Sent: List<String>?
): Notification.Builder {
    val recipient: android.app.Person = android.app.Person.Builder().setName("Darren").setImportant(true).build()
    val you: android.app.Person? = null

    val messageStyle = Notification.MessagingStyle(recipient)
    val message1 = Notification.MessagingStyle.Message("Hello!", Instant.now().minusSeconds(10 * 60).toEpochMilli(), recipient)
    messageStyle.addHistoricMessage(message1)
    messageStyle.addMessage(Notification.MessagingStyle.Message("Hi", Instant.now().toEpochMilli(), recipient))

    val remoteInput: android.app.RemoteInput = android.app.RemoteInput.Builder(upcoming.id.toString()).run {
        top3Sent?.let { setChoices(top3Sent.toTypedArray()) }
        build()
    }

    //...

    val inputAction = Notification.Action.Builder(0, context.getString(R.string.button_edit), inputPending).run {
        addRemoteInput(remoteInput)
        build()
    }

    return Notification.Builder(context, "Input").apply {
        setSmallIcon(R.drawable.ic_stat)
        style = messageStyle
        setAutoCancel(true)
        setCategory(Notification.CATEGORY_REMINDER)
        setColor(ContextCompat.getColor(context, R.color.secondaryDarkColor))
        setContentIntent(contentPending)
        setDeleteIntent(deletePending)
        setGroup("notifications")
        setOnlyAlertOnce(true)
        setVisibility(Notification.VISIBILITY_PRIVATE)
        addAction(inputAction)
    }
}
like image 935
Bink Avatar asked May 12 '26 19:05

Bink


1 Answers

This is the behavior of historic message

Historic message is not normally shown at the notification. It is a special message that is only shown when user is replying through a RemoteInput. See the image above to see the behaviour. It should only be used when the message is not the main subject of the notification but may give context to a conversation.

Reference: Android MessagingStyle Notification As Clear As Possible

like image 76
Myrick Chow Avatar answered May 15 '26 09:05

Myrick Chow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!