Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 홈 채팅 구현 #243

Open
wants to merge 36 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6ff64f5
feature: Add firebase messaging setting
leeseokchan00 Nov 12, 2024
0747a3c
feature: Add device token local save
leeseokchan00 Nov 12, 2024
9633596
Merge branch 'develop' of https://github.com/Team-Offroad/Offroad-And…
leeseokchan00 Nov 12, 2024
314ec2c
Merge branch 'develop' of https://github.com/Team-Offroad/Offroad-And…
leeseokchan00 Nov 12, 2024
80aabc1
refactor: Edit emblem scroll
leeseokchan00 Nov 13, 2024
4784253
feature: Add fcm
leeseokchan00 Nov 13, 2024
0278b98
feature: Add fcm to main
leeseokchan00 Nov 15, 2024
02a385d
feature: Add fcm background click
leeseokchan00 Nov 16, 2024
7a398ea
feature: Add chat exist
OliviaYJH Nov 16, 2024
d90884d
feature: Add ChatTextField
OliviaYJH Nov 16, 2024
ecbd62f
feature: Add fcm navigate to mypage
leeseokchan00 Nov 16, 2024
1d25ca1
Merge branch 'develop' of https://github.com/Team-Offroad/Offroad-And…
leeseokchan00 Nov 16, 2024
afb049a
feature: Edit ChatTextField
OliviaYJH Nov 16, 2024
160232e
refactor: Edit home ui
OliviaYJH Nov 16, 2024
45f50af
refactor: Edit keyboard
OliviaYJH Nov 16, 2024
e3ca4d7
feature: Add finish chatting button
OliviaYJH Nov 16, 2024
b156f07
feature: Add textfield onValueChange
OliviaYJH Nov 16, 2024
2bb4841
Merge branch 'develop' into feature/215
OliviaYJH Nov 16, 2024
ee666be
feature: Add character chat broadcast receiver
leeseokchan00 Nov 16, 2024
16127c0
Merge branch 'develop' into feature/215
OliviaYJH Nov 17, 2024
669dba7
refactor: Edit broadcast receiver
leeseokchan00 Nov 17, 2024
9959067
Merge branch 'develop' of https://github.com/Team-Offroad/Offroad-And…
leeseokchan00 Nov 17, 2024
bb5bcf8
feature: Add character chat api
OliviaYJH Nov 17, 2024
c5404a2
refactor: Edit my chatting text
OliviaYJH Nov 17, 2024
09d6a58
Merge branch 'develop' into feature/215
OliviaYJH Nov 17, 2024
bb206be
refactor: Delete imports
OliviaYJH Nov 17, 2024
fb369ee
Merge branch 'feature/219' into feature/215
OliviaYJH Nov 17, 2024
e6b9c93
feature: Add home character chat
OliviaYJH Nov 17, 2024
5e8bcb8
feature: Add Character Chat
OliviaYJH Nov 17, 2024
54a33c2
feature: Add isCharacterChattingLoading
OliviaYJH Nov 17, 2024
9423e7e
refactor: Edit home character image url
OliviaYJH Nov 17, 2024
8276374
feature: Add update isCharacterChatting
OliviaYJH Nov 17, 2024
b673359
feature: Add ic_home_accordion
OliviaYJH Nov 18, 2024
af89b84
feature: Add accordion rotationX
OliviaYJH Nov 18, 2024
ddf0361
feature: Connect home and characterchat
OliviaYJH Nov 18, 2024
062d3c1
feature: Add animation to character chat
OliviaYJH Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.teamoffroad.app.setNamespace

plugins {
id("offroad.android.application")
alias(libs.plugins.google.services)
}

android {
Expand Down Expand Up @@ -55,4 +56,7 @@ dependencies {
implementation(project(":feature:mypage"))
implementation(project(":feature:characterchat"))
implementation(libs.kakao.user)
implementation(libs.coil)
implementation(libs.bundles.firebase)
implementation(platform(libs.firebase.bom))
}
14 changes: 13 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:name=".OffroadApplication"
Expand All @@ -23,7 +24,13 @@
android:theme="@style/Theme.Offroad"
android:usesCleartextTraffic="true"
tools:targetApi="31">

<service
android:name=".OffRoadMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.naver.maps.map.CLIENT_ID"
android:value="${NAVER_CLIENT_ID}" />
Expand All @@ -43,5 +50,10 @@
</intent-filter>

</activity>
<receiver android:name="com.teamoffroad.feature.main.CharacterChatBroadcastReceiver">
<intent-filter>
<action android:name="com.teamoffroad.offroad.app.ACTION_RECEIVE_NOTIFICATION" />
</intent-filter>
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.teamoffroad.offroad.app

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import androidx.core.app.NotificationCompat
import coil.Coil
import coil.request.ImageRequest
import com.google.firebase.messaging.Constants.MessageNotificationKeys
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.CHANNEL_ID
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.KEY_BODY
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.KEY_ID
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.KEY_IMAGE
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.KEY_TITLE
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.KEY_TYPE
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.NOTICE
import com.teamoffroad.core.common.domain.model.FcmNotificationKey.TYPE_CHARACTER_CHAT
import com.teamoffroad.core.common.domain.repository.DeviceTokenRepository
import com.teamoffroad.core.common.util.ActivityLifecycleHandler
import com.teamoffroad.feature.main.MainActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class OffRoadMessagingService : FirebaseMessagingService() {
@Inject
lateinit var dataStore: DeviceTokenRepository

override fun onNewToken(token: String) {
super.onNewToken(token)

CoroutineScope(Dispatchers.IO).launch { dataStore.updateDeviceTokenEnabled(token) }
}

//FCM 메세지를 받을 때 호출됨
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
if (remoteMessage.data.isNotEmpty()) {
if (ActivityLifecycleHandler.isAppInForeground) {
if (remoteMessage.data[KEY_TYPE] != TYPE_CHARACTER_CHAT)
sendNotification(remoteMessage, true)
else {
// 앱이 포그라운드에 있고, 알림타임이 캐릭터채팅인 경우
// 정현이 봐야할곳은 여기!! 요쪽 따라가십쇼
sendCharacterChatNotificationInForeground(remoteMessage)
}
} else {
sendNotification(remoteMessage, false)
}
}
}

override fun handleIntent(intent: Intent?) {
val new = intent?.apply {
val temp = extras?.apply {
remove(MessageNotificationKeys.ENABLE_NOTIFICATION)
remove(keyWithOldPrefix())
}
replaceExtras(temp)
}
super.handleIntent(new)
}

private fun keyWithOldPrefix(): String {
if (!MessageNotificationKeys.ENABLE_NOTIFICATION.startsWith(MessageNotificationKeys.NOTIFICATION_PREFIX)) {
return MessageNotificationKeys.ENABLE_NOTIFICATION
}

return MessageNotificationKeys.ENABLE_NOTIFICATION.replace(
MessageNotificationKeys.NOTIFICATION_PREFIX,
MessageNotificationKeys.NOTIFICATION_PREFIX_OLD
)
}

private fun generateUniqueIdentifier(): Int {
return (System.currentTimeMillis() / 7).toInt()
}

private fun createPendingIntent(intent: Intent, uniqueIdentifier: Int): PendingIntent {
return PendingIntent.getActivity(
this,
uniqueIdentifier,
intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
}

private fun createNotificationIntent(
remoteMessage: RemoteMessage,
isForeGround: Boolean
): Intent {
return Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(KEY_TYPE, remoteMessage.data[KEY_TYPE])
if (remoteMessage.data[KEY_TYPE] != TYPE_CHARACTER_CHAT) {
putExtra(KEY_ID, remoteMessage.data[KEY_ID])

}
}
}

private fun createNotificationBuilder(
remoteMessage: RemoteMessage,
pendingIntent: PendingIntent,
onLargeIconReady: (NotificationCompat.Builder) -> Unit
): NotificationCompat.Builder {

val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.data[KEY_TITLE])
.setContentText(remoteMessage.data[KEY_BODY])
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)

val imageUrl = remoteMessage.data[KEY_IMAGE]
imageUrl?.let {
val request = ImageRequest.Builder(this)
.data(it)
.target { drawable ->
val bitmap = (drawable as BitmapDrawable).bitmap
notificationBuilder.setLargeIcon(bitmap)
onLargeIconReady(notificationBuilder)
}
.build()

Coil.imageLoader(this).enqueue(request)
} ?: run {
onLargeIconReady(notificationBuilder)
}
return notificationBuilder
}

private fun sendNotification(remoteMessage: RemoteMessage, isForeGround: Boolean) {
if (!isForeGround) {
val uniqueIdentifier = generateUniqueIdentifier()
val intent = createNotificationIntent(remoteMessage, isForeGround)
val pendingIntent = createPendingIntent(intent, uniqueIdentifier)
createNotificationBuilder(remoteMessage, pendingIntent) { notificationBuilder ->
showNotification(notificationBuilder, uniqueIdentifier)
}
} else {
val uniqueIdentifier = generateUniqueIdentifier()
val broadCastIntent =
Intent("com.teamoffroad.offroad.app.ANNOUNCEMENT_FOREGROUND").apply {
putExtra(KEY_TITLE, remoteMessage.data[KEY_TITLE])
putExtra(KEY_ID, remoteMessage.data[KEY_ID])
}
val pendingIntent = PendingIntent.getBroadcast(
this,
0,
broadCastIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // FLAG_IMMUTABLE 추가
)
createNotificationBuilder(remoteMessage, pendingIntent) { notificationBuilder ->
showNotification(notificationBuilder, uniqueIdentifier)
}
}
}

//브로드캐스트리시버에 필요한 데이터(캐릭터이름, 대화내용, 알림타입) 저장하고 브로드캐스트 발신
//feature main의 CharacterChatBroadcastReceiver로 가면 됩니다.
private fun sendCharacterChatNotificationInForeground(
remoteMessage: RemoteMessage,
) {
val broadCastIntent =
Intent("com.teamoffroad.offroad.app.CHARACTER_CHAT_FOREGROUND").apply {
putExtra(KEY_TITLE, remoteMessage.data[KEY_TITLE])
putExtra(KEY_BODY, remoteMessage.data[KEY_BODY])
putExtra(KEY_TYPE, remoteMessage.data[KEY_TYPE])
}
sendBroadcast(broadCastIntent)
}

private fun showNotification(
notificationBuilder: NotificationCompat.Builder,
uniqueIdentifier: Int
) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(CHANNEL_ID, NOTICE, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}

notificationManager.notify(uniqueIdentifier, notificationBuilder.build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.teamoffroad.offroad.app

import android.app.Application
import com.kakao.sdk.common.KakaoSdk
import com.teamoffroad.core.common.util.ActivityLifecycleHandler
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class OffroadApplication : Application(){
override fun onCreate() {
super.onCreate()
setKakaoSdk()
registerActivityLifecycleCallbacks(ActivityLifecycleHandler())
}

private fun setKakaoSdk() {
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ plugins {
alias(libs.plugins.android.library) apply false
alias(libs.plugins.daggers.hilt) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.google.services) apply false
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ import kotlinx.coroutines.flow.Flow

interface AutoSignInPreferencesDataSource {
val autoLogin: Flow<Boolean>

suspend fun setAutoLogin(autoLogin: Boolean)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.teamoffroad.core.common.data.datasource

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class DefaultDeviceTokenPreferencesDataSource @Inject constructor(
private val dataStore: DataStore<Preferences>,
) : DeviceTokenPreferencesDataSource {

object PreferencesKey {
val DEVICE_TOKEN_KEY = stringPreferencesKey("DEVICE_TOKEN_KEY")
}

override val deviceToken: Flow<String> = dataStore.data.map { preferences ->
preferences[PreferencesKey.DEVICE_TOKEN_KEY].orEmpty()
}

override suspend fun setDeviceToken(deviceToken: String) {
dataStore.edit { preferences ->
preferences[PreferencesKey.DEVICE_TOKEN_KEY] = deviceToken
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.teamoffroad.core.common.data.datasource

import kotlinx.coroutines.flow.Flow

interface DeviceTokenPreferencesDataSource {
val deviceToken: Flow<String>
suspend fun setDeviceToken(deviceToken: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.teamoffroad.core.common.data.di

import com.teamoffroad.core.common.data.datasource.AutoSignInPreferencesDataSource
import com.teamoffroad.core.common.data.datasource.DefaultAutoSignInPreferencesDataSource
import com.teamoffroad.core.common.data.datasource.DefaultDeviceTokenPreferencesDataSource
import com.teamoffroad.core.common.data.datasource.DefaultTokenPreferencesDataSource
import com.teamoffroad.core.common.data.datasource.DeviceTokenPreferencesDataSource
import com.teamoffroad.core.common.data.datasource.TokenPreferencesDataSource
import dagger.Binds
import dagger.Module
Expand All @@ -22,4 +24,9 @@ internal abstract class DataModule {
abstract fun bindsAutoSignInLocalDataSource(
dataSource: DefaultAutoSignInPreferencesDataSource,
): AutoSignInPreferencesDataSource

@Binds
abstract fun bindsDeviceTokenLocalDataSource(
dataSource: DefaultDeviceTokenPreferencesDataSource,
): DeviceTokenPreferencesDataSource
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ object DataStoreModule {
return context.createDataStore(AUTH_PREFERENCES)
}

@Provides
@Singleton
fun provideDeviceTokenDataStore(@ApplicationContext context: Context): DataStore<Preferences> {
return context.createDataStore(DEVICE_TOKEN_PREFERENCES)
}

private const val TOKEN_PREFERENCES = "com.teamoffroad.token_preferences"
private const val AUTH_PREFERENCES = "com.teamoffroad.auth_preferences"
private const val DEVICE_TOKEN_PREFERENCES = "com.teamoffroad.device_token_preferences"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.teamoffroad.core.common.data.di

import com.teamoffroad.core.common.data.repository.AutoSignInRepositoryImpl
import com.teamoffroad.core.common.data.repository.DeviceTokenRepositoryImpl
import com.teamoffroad.core.common.data.repository.TokenRepositoryImpl
import com.teamoffroad.core.common.domain.repository.AutoSignInRepository
import com.teamoffroad.core.common.domain.repository.DeviceTokenRepository
import com.teamoffroad.core.common.domain.repository.TokenRepository
import dagger.Binds
import dagger.Module
Expand All @@ -24,4 +26,10 @@ abstract class RepositoryModule {
abstract fun bindAutoSignInRepository(
authRepositoryImpl: AutoSignInRepositoryImpl,
): AutoSignInRepository

@Binds
@Singleton
abstract fun bindDeviceTokenRepository(
deviceTokenRepositoryImpl: DeviceTokenRepositoryImpl
): DeviceTokenRepository
}
Loading