Hardcode notification to be displayed on launch, for most recent conversation

This commit is contained in:
Moez Bhatti
2017-09-21 22:00:20 -04:00
parent 5e3f7bbfaa
commit a45b502656
3 changed files with 76 additions and 0 deletions
@@ -5,6 +5,7 @@ import android.content.Context
import com.moez.QKSMS.data.repository.ContactRepository
import com.moez.QKSMS.data.sync.SyncManager
import com.moez.QKSMS.util.DateFormatter
import com.moez.QKSMS.util.NotificationHelper
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
@@ -30,4 +31,10 @@ class AppModule(var application: Application) {
return SyncManager(context, contacts)
}
@Provides
@Singleton
fun provideNotificationHelper(context: Context): NotificationHelper {
return NotificationHelper(context)
}
}
@@ -6,6 +6,7 @@ import com.moez.QKSMS.dagger.AppComponentManager
import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.repository.ConversationRepository
import com.moez.QKSMS.data.sync.SyncManager
import com.moez.QKSMS.util.NotificationHelper
import io.reactivex.subjects.PublishSubject
import io.realm.RealmResults
import javax.inject.Inject
@@ -13,6 +14,7 @@ import javax.inject.Inject
class ConversationListViewModel : ViewModel() {
@Inject lateinit var syncManager: SyncManager
@Inject lateinit var notificationManager: NotificationHelper
@Inject lateinit var conversationRepo: ConversationRepository
val state: MutableLiveData<ConversationListViewState> = MutableLiveData()
@@ -23,6 +25,8 @@ class ConversationListViewModel : ViewModel() {
init {
AppComponentManager.appComponent.inject(this)
notificationManager.update()
partialStates
.scan(ConversationListViewState(), { previous, changes -> changes.reduce(previous) })
.subscribe { newState -> state.value = newState }
@@ -0,0 +1,65 @@
package com.moez.QKSMS.util
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
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.data.model.Conversation
import com.moez.QKSMS.data.model.Message
import io.realm.Realm
class NotificationHelper(val context: Context) {
private val notificationManager = NotificationManagerCompat.from(context)
init {
if (Build.VERSION.SDK_INT >= 26) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val id = "channel_1"
val name = "Message notifications"
val description = "Message notifications description"
val importance = NotificationManager.IMPORTANCE_MAX
val channel = NotificationChannel(id, name, importance)
channel.description = description
channel.enableLights(true)
channel.lightColor = Color.WHITE
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
notificationManager.createNotificationChannel(channel)
}
}
fun update() {
val realm = Realm.getDefaultInstance()
val conversation = realm.where(Conversation::class.java).findFirst()
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")
messages.take(NotificationCompat.MessagingStyle.MAXIMUM_RETAINED_MESSAGES).forEach { message ->
style.addMessage(message.body, message.date, if (message.isMe()) null else "Person")
}
val notification = NotificationCompat.Builder(context, "channel_1")
.setColor(context.resources.getColor(R.color.colorPrimary))
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(style)
notificationManager.notify(0, notification.build())
}
realm.close()
}
}