gpt4 book ai didi

android - MessagingStyle 不显示文本

转载 作者:行者123 更新时间:2023-12-02 12:25:00 25 4
gpt4 key购买 nike

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

最佳答案

This is the behavior of historic message

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

引用:Android MessagingStyle Notification As Clear As Possible

关于android - MessagingStyle 不显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54082631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com