I’ve been developing an Android forum app in Kotlin, using Jetpack Compose. I’d like to send notifications to those who have replied to some specific post.
First, I’d like to get the uids and tokens of them(fun getOthersToken), and then send notifications to them(fun createNotification). However, I cannot get each uid of them and I have an error because "uid" in (uid in thoseWhoRepliedEntity.uid) is Char type, not String.
This is the current code.
var thoseWhoReplied = mutableListOf<ThoseWhoRepliedEntity>()
val service: UserService = retrofit.create(
UserService::class.java
)
val apiResponse = service.getOthersToken(token, postId)
.execute().body()
?: throw IllegalStateException("body is null.")
for (info in apiResponse) {
val thoseWhoRepliedEntity = ThoseWhoRepliedEntity(
info.uid,
info.token
)
thoseWhoReplied.add(thoseWhoRepliedEntity)
try {
val service: NotificationService =
retrofit.create(NotificationService::class.java)
for (uid in thoseWhoRepliedEntity.uid) {
val notification = NotificationEntity(
uid,
"You have a reply.",
"${LocalDateTime.now()}"
)
service.createNotification(token, notification)
.enqueue(object : Callback<ResponseBody> {
override fun onResponse(
call: Call<ResponseBody>,
response: Response<ResponseBody>
) {
Log.d(
"Response is",
"${response.body()}"
)
}
override fun onFailure(
call: Call<ResponseBody>,
t: Throwable
) {
Log.d("Hi", "error")
}
})
}
ThoseWhoRepliedEntity
data class ThoseWhoRepliedEntity(
var uid: String = "",
var token: String = ""
)
Could someone help me? Thank you.
>Solution :
You are getting Char elements and not a expected String because you are iterating over content of your uid strings (thus getting its characters), so you are one step too deep. So instead of:
for (uid in thoseWhoRepliedEntity.uid) {
val notification = NotificationEntity(
uid,
"You have a reply.",
"${LocalDateTime.now()}"
)
...
}
you need to write:
for (entity in thoseWhoReplied) {
val notification = NotificationEntity(
entity.uid,
"You have a reply.",
"${LocalDateTime.now()}"
)
....
}