MessagingStyle不显示文本

时间:2019-01-07 22:15:05

标签: android kotlin android-notifications

Android开发的新手,我正在尝试最新的addHistoricMessage,但由于它没有显示任何内容,我缺少了一些东西。在极少数情况下会显示addMessage文本,但永远不会显示addHistoricMessageaddMessage在使用NotificationCompat时始终如一地工作,但是NotificationCompat似乎没有addHistoricMessage

任何想法都得到赞赏-使用androidx.appcompat:appcompat:1.0.2compileSdkVersiontargetSdkVersion都是28。

我看到的一个例子是

Notification without MessagingStyle messages

调用通知的测试按钮:

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!")
        )
    }
}

通知方法和相关的(删除了不太重要的代码):

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)
    }
}

1 个答案:

答案 0 :(得分:1)

This is the behavior of historic message

历史消息通常不会显示在通知中。这是一条特殊消息,仅在用户通过RemoteInput答复时显示。参见上图查看行为。仅当消息不是通知的主要主题但可能为对话提供上下文时,才应使用它。

参考:Android MessagingStyle Notification As Clear As Possible