MessagingStyle 未正确显示 "You"
MessagingStyle Not Displaying "You" Properly
Android 开发新手,使用最新的 MessagingStyle
代码。来自你的消息显示正确的图标,但是,对于名字,我期待你而不是收件人的名字,就像内置的消息应用程序一样。不确定这是否与 Kotlin 相关。
任何想法表示赞赏 - compileSdkVersion
和 targetSdkVersion
都是 28。
我看到的例子是:
通知方法及相关(删除不太重要的代码):
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)
}
}
你必须做两件事:
- 将 You-person 传递给
Notification.MessagingStyle
构造函数,而不是接收者。
- 将 null 传递给来自 You-person 的每个
Notification.MessagingStyle.Message
。由于新的 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`
Android 开发新手,使用最新的 MessagingStyle
代码。来自你的消息显示正确的图标,但是,对于名字,我期待你而不是收件人的名字,就像内置的消息应用程序一样。不确定这是否与 Kotlin 相关。
任何想法表示赞赏 - compileSdkVersion
和 targetSdkVersion
都是 28。
我看到的例子是:
通知方法及相关(删除不太重要的代码):
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)
}
}
你必须做两件事:
- 将 You-person 传递给
Notification.MessagingStyle
构造函数,而不是接收者。 - 将 null 传递给来自 You-person 的每个
Notification.MessagingStyle.Message
。由于新的 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`