Display notifications for incoming messages, mark seen when notification dismissed

This commit is contained in:
Moez Bhatti
2017-09-26 13:55:14 -04:00
parent d88bdf679d
commit 9c826ff71e
6 changed files with 92 additions and 26 deletions
+1
View File
@@ -61,6 +61,7 @@
</receiver>
<receiver android:name=".receiver.MessageSentReceiver" />
<receiver android:name=".receiver.MessageDeliveredReceiver" />
<receiver android:name=".receiver.MarkSeenReceiver" />
<service android:name=".service.HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
@@ -1,5 +1,6 @@
package com.moez.QKSMS.dagger
import com.moez.QKSMS.receiver.MarkSeenReceiver
import com.moez.QKSMS.receiver.MessageSentReceiver
import com.moez.QKSMS.receiver.SmsReceiver
import com.moez.QKSMS.ui.conversations.ConversationAdapter
@@ -17,6 +18,7 @@ interface AppComponent {
fun inject(receiver: SmsReceiver)
fun inject(receiver: MessageSentReceiver)
fun inject(receiver: MarkSeenReceiver)
fun inject(activity: MessageListActivity)
@@ -41,14 +41,14 @@ class AppModule(var application: Application) {
@Provides
@Singleton
fun provideNotificationHelper(context: Context, messageRepository: MessageRepository): NotificationManager {
return NotificationManager(context, messageRepository)
fun provideNotificationHelper(context: Context, themeManager: ThemeManager): NotificationManager {
return NotificationManager(context, themeManager)
}
@Singleton
@Provides
fun provideMessageRepository(context: Context): MessageRepository {
return MessageRepository(context)
fun provideMessageRepository(context: Context, notificationManager: NotificationManager): MessageRepository {
return MessageRepository(context, notificationManager)
}
@Provides
@@ -14,6 +14,7 @@ import com.moez.QKSMS.data.sync.MessageColumns
import com.moez.QKSMS.data.sync.SyncManager
import com.moez.QKSMS.receiver.MessageDeliveredReceiver
import com.moez.QKSMS.receiver.MessageSentReceiver
import com.moez.QKSMS.util.NotificationManager
import com.moez.QKSMS.util.extensions.asFlowable
import com.moez.QKSMS.util.extensions.insertOrUpdate
import io.reactivex.Flowable
@@ -22,7 +23,7 @@ import io.realm.Realm
import io.realm.RealmResults
import io.realm.Sort
class MessageRepository(val context: Context) {
class MessageRepository(val context: Context, val notificationManager: NotificationManager) {
fun getConversationMessagesAsync(): RealmResults<Message> {
return Realm.getDefaultInstance()
@@ -70,7 +71,10 @@ class MessageRepository(val context: Context) {
Flowable.just(values)
.subscribeOn(Schedulers.io())
.map { contentResolver.insert(Sms.Inbox.CONTENT_URI, values) }
.subscribe { uri -> copyMessageToRealm(uri) }
.subscribe { uri ->
copyMessageToRealm(uri)
notificationManager.update(this)
}
}
fun markAllSeen() {
@@ -100,6 +104,41 @@ class MessageRepository(val context: Context) {
}
}
fun markSeen(threadId: Long) {
// Messages in SMS ContentProvider
val projection = arrayOf(BaseColumns._ID)
val selection = "${Sms.THREAD_ID} = $threadId AND ${Sms.SEEN} = 0"
val contentResolver = context.contentResolver
contentResolver.query(Sms.Inbox.CONTENT_URI, projection, selection, null, null)
.asFlowable()
.subscribeOn(Schedulers.io())
.map { cursor -> cursor.getLong(0) }
.map { id -> Uri.withAppendedPath(Sms.CONTENT_URI, id.toString()) }
.subscribe { uri ->
val values = ContentValues()
values.put(Sms.SEEN, true)
contentResolver.update(uri, values, null, null)
}
// TODO also need to mark MMS in ContentProvider as Seen
// Messages in Realm
Schedulers.io().scheduleDirect {
val realm = Realm.getDefaultInstance()
val messages = realm.where(Message::class.java)
.equalTo("threadId", threadId)
.equalTo("seen", false)
.findAll()
realm.executeTransaction {
messages.forEach { message ->
message.seen = true
}
}
realm.close()
}
}
fun markRead(threadId: Long) {
// Messages in SMS ContentProvider
val projection = arrayOf(BaseColumns._ID)
@@ -0,0 +1,21 @@
package com.moez.QKSMS.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.moez.QKSMS.dagger.AppComponentManager
import com.moez.QKSMS.data.repository.MessageRepository
import javax.inject.Inject
class MarkSeenReceiver : BroadcastReceiver() {
@Inject lateinit var messageRepo: MessageRepository
override fun onReceive(context: Context, intent: Intent) {
AppComponentManager.appComponent.inject(this)
val threadId = intent.getLongExtra("threadId", 0)
messageRepo.markSeen(threadId)
}
}
@@ -2,26 +2,23 @@ package com.moez.QKSMS.util
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import com.moez.QKSMS.R
import com.moez.QKSMS.dagger.AppComponentManager
import com.moez.QKSMS.data.repository.MessageRepository
import javax.inject.Inject
import com.moez.QKSMS.receiver.MarkSeenReceiver
class NotificationManager(val context: Context, val messageRepo: MessageRepository) {
@Inject lateinit var themeManager: ThemeManager
class NotificationManager(private val context: Context, private val themeManager: ThemeManager) {
private val notificationManager = NotificationManagerCompat.from(context)
init {
AppComponentManager.appComponent.inject(this)
if (Build.VERSION.SDK_INT >= 26) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@@ -42,22 +39,28 @@ class NotificationManager(val context: Context, val messageRepo: MessageReposito
}
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html
fun update() {
messageRepo.getUnreadUnseenMessages().groupBy { message -> message.threadId }.forEach { conversation ->
fun update(messageRepo: MessageRepository) {
messageRepo.getUnreadUnseenMessages()
.groupBy { message -> message.threadId }
.forEach { conversation ->
val style = NotificationCompat.MessagingStyle("Me")
conversation.value.forEach { message ->
val name = if (message.isMe()) null else "Person"
style.addMessage(message.body, message.date, name)
}
val style = NotificationCompat.MessagingStyle("Me")
conversation.value.forEach { message ->
val name = if (message.isMe()) null else "Person"
style.addMessage(message.body, message.date, name)
}
val notification = NotificationCompat.Builder(context, "channel_1")
.setColor(themeManager.color)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(style)
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", conversation.key)
val seenPI = PendingIntent.getBroadcast(context, conversation.key.toInt(), seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationManager.notify(conversation.key.toInt(), notification.build())
}
val notification = NotificationCompat.Builder(context, "channel_1")
.setColor(themeManager.color)
.setSmallIcon(R.mipmap.ic_launcher)
.setDeleteIntent(seenPI)
.setStyle(style)
notificationManager.notify(conversation.key.toInt(), notification.build())
}
}
}