diff --git a/demo/src/main/java/com/tinder/service/ChatRoomSocketIoService.kt b/demo/src/main/java/com/tinder/service/ChatRoomSocketIoService.kt index e570a822..1b6fcff3 100644 --- a/demo/src/main/java/com/tinder/service/ChatRoomSocketIoService.kt +++ b/demo/src/main/java/com/tinder/service/ChatRoomSocketIoService.kt @@ -4,13 +4,16 @@ package com.tinder.service +import android.annotation.SuppressLint import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.content.Intent +import android.os.Build import android.os.Handler import android.os.IBinder import android.os.Messenger +import androidx.core.app.NotificationCompat import androidx.lifecycle.LifecycleService import com.tinder.R import com.tinder.app.socketio.chatroom.api.AddUserTopic @@ -36,6 +39,7 @@ class ChatRoomSocketIoService : LifecycleService() { var incomingMessageCount = 0 private val messenger = Messenger(IncomingHandler()) + @SuppressLint("CheckResult") override fun onCreate() { super.onCreate() @@ -73,9 +77,11 @@ class ChatRoomSocketIoService : LifecycleService() { val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - val notificationChannel = - NotificationChannel(channelId, "Default", NotificationManager.IMPORTANCE_HIGH) - notificationManager.createNotificationChannel(notificationChannel) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notificationChannel = + NotificationChannel(channelId, "Default", NotificationManager.IMPORTANCE_HIGH) + notificationManager.createNotificationChannel(notificationChannel) + } Timber.d("chatroom scarlet created") @@ -94,37 +100,42 @@ class ChatRoomSocketIoService : LifecycleService() { addUserTopic.observeSocketIoEvent() .filter { it is SocketIoEvent.OnConnectionOpened } .observeOn(Schedulers.io()) - .subscribe({ - addUserTopic.sendAddUser(username) - - Timber.d("chatroom added user: $it") - val notification = createNotification { - setContentText("Joined chatroom") - } - notificationManager.notify(notificationIdCurrentState, notification) - }, { e -> - Timber.e(e) - }) + .subscribe( + { + addUserTopic.sendAddUser(username) + + Timber.d("chatroom added user: $it") + val notification = createNotification { + setContentText("Joined chatroom") + } + notificationManager.notify(notificationIdCurrentState, notification) + }, + { e -> + Timber.e(e) + }) Flowable.fromPublisher(AndroidLifecycle.ofLifecycleServiceStarted(application, this)) - .subscribe({ + .subscribe { Timber.d("chatroom lifecycle: $it") val notification = createNotification { setContentText("Lifecycle State: $it") } notificationManager.notify(notificationIdCurrentState, notification) - }) + } Flowable.merge( - newMessageTopic.observeNewMessage().map { "${it.username}: ${it.message}" }, - typingStartedTopic.observeTypingStarted().map { "${it.username} started typing" }, - typingStoppedTopic.observeTypingStopped().map { "${it.username} stopped typing" } + newMessageTopic.observeNewMessage() + .map { "${it.username}: ${it.message}" }, + typingStartedTopic.observeTypingStarted() + .map { "${it.username} started typing" }, + typingStoppedTopic.observeTypingStopped() + .map { "${it.username} stopped typing" } ).subscribe { Timber.d("chatroom new message: $it") val notification = createNotification { - setContentText("$it") + setContentText(it) } val notificationId = notificationIdIncomingMessage + incomingMessageCount @@ -139,8 +150,8 @@ class ChatRoomSocketIoService : LifecycleService() { return messenger.binder } - private fun createNotification(builder: Notification.Builder.() -> Unit): Notification { - return Notification.Builder(applicationContext, channelId) + private fun createNotification(builder: NotificationCompat.Builder.() -> Unit): Notification { + return NotificationCompat.Builder(applicationContext, channelId) .setSmallIcon(R.drawable.ic_action_info) .setWhen(System.currentTimeMillis()) .setContentTitle("Chat Room") diff --git a/dependencies.gradle b/dependencies.gradle index ff72df7b..c7a79c2c 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -3,7 +3,7 @@ */ ext { - minSdkVersion = 28 + minSdkVersion = 24 minSdkVersionAndroidLifecycle = 19 targetSdkVersion = 28 compileSdkVersion = 28 diff --git a/gradle.properties b/gradle.properties index 2dda6e76..53371d19 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,4 +4,4 @@ android.enableJetifier=true android.useAndroidX=true -version=0.2.4-alpha1 +version=0.2.4