MessagingStyle 不显示文本

宾克

Android 开发新手,我正在尝试最新的addHistoricMessage,但我错过了一些东西,因为它没有显示任何内容。在极少数情况下addMessage显示文本,但从addHistoricMessage不显示。addMessage使用时始终如一地工作NotificationCompat,但NotificationCompat似乎没有addHistoricMessage.

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

我所看到的一个例子是:

没有 MessagingStyle 消息的通知

调用通知的测试按钮:

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)
    }
}
麦瑞克·周

这是历史消息的行为

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

参考:Android MessagingStyle 通知尽可能清晰

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章