forked from upstream-mirrors/quik
Show notifications for all unread conversations
This commit is contained in:
@@ -11,6 +11,7 @@ import com.moez.QKSMS.R
|
|||||||
import com.moez.QKSMS.data.model.Conversation
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import io.realm.Sort
|
||||||
|
|
||||||
|
|
||||||
class NotificationHelper(val context: Context) {
|
class NotificationHelper(val context: Context) {
|
||||||
@@ -37,26 +38,28 @@ class NotificationHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html
|
||||||
fun update() {
|
fun update() {
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
|
|
||||||
val conversation = realm.where(Conversation::class.java).findFirst()
|
realm.where(Conversation::class.java).equalTo("read", false).findAllSorted("date").forEach { conversation ->
|
||||||
if (conversation != null) {
|
|
||||||
val messages = realm.where(Message::class.java).equalTo("threadId", conversation.id).findAllSorted("date")
|
|
||||||
|
|
||||||
// A conversation title can also be set here
|
|
||||||
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html
|
|
||||||
val style = NotificationCompat.MessagingStyle("Me")
|
val style = NotificationCompat.MessagingStyle("Me")
|
||||||
messages.take(NotificationCompat.MessagingStyle.MAXIMUM_RETAINED_MESSAGES).forEach { message ->
|
realm.where(Message::class.java)
|
||||||
style.addMessage(message.body, message.date, if (message.isMe()) null else "Person")
|
.equalTo("threadId", conversation.id)
|
||||||
}
|
.findAllSorted("date", Sort.DESCENDING)
|
||||||
|
.take(2)
|
||||||
|
.reversed()
|
||||||
|
.forEach { message ->
|
||||||
|
val name = if (message.isMe()) null else conversation.getTitle()
|
||||||
|
style.addMessage(message.body, message.date, name)
|
||||||
|
}
|
||||||
|
|
||||||
val notification = NotificationCompat.Builder(context, "channel_1")
|
val notification = NotificationCompat.Builder(context, "channel_1")
|
||||||
.setColor(context.resources.getColor(R.color.colorPrimary))
|
.setColor(context.resources.getColor(R.color.colorPrimary))
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
.setStyle(style)
|
.setStyle(style)
|
||||||
|
|
||||||
notificationManager.notify(0, notification.build())
|
notificationManager.notify(conversation.id.toInt(), notification.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
realm.close()
|
realm.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user