MessagingStyle无法正确显示“您”

时间:2019-01-09 18:10:08

标签: android kotlin android-notifications

Android开发的新手,并使用最新的MessagingStyle代码。与您的邮件一起显示的是正确的图标,但是,就像内置的“邮件”应用程序一样,我希望您使用的名称不是收件人的姓名,而是您的名字。不知道这是否与Kotlin有关。

任何想法都值得赞赏-compileSdkVersiontargetSdkVersion都28岁。

我看到的一个例子是

enter image description here

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

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)
    messageStyle.addMessage(Notification.MessagingStyle.Message("What's up?", Instant.now().minusSeconds(5 * 60).toEpochMilli(), recipient))
    messageStyle.addMessage(Notification.MessagingStyle.Message("Doing great!", Instant.now().toEpochMilli(), you))

    //...

    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)

您必须做两件事:

  1. 将您的用户传递给Notification.MessagingStyle构造函数,而不是接收者。
  2. 向您的每个Notification.MessagingStyle.Message传递一个null。由于新API,Kotlin中存在一个小问题,因为它的类型为Person,而不是Person?

我刚刚检查了这段代码,它的工作方式就像您想要的:

val recipient: Person = Person.Builder().setName("Darren").build()
val you: Person = Person.Builder().setName("You").build()
val nullPerson: Person? = null

val messageStyle = Notification.MessagingStyle(you) // pass you-person here
messageStyle.addMessage(Notification.MessagingStyle.Message("What's up?", timestamp1, recipient))
messageStyle.addMessage(Notification.MessagingStyle.Message("Doing great!", timestamp2, nullPerson)) // pass to null for a message to display `You`