-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,389 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/comit/simtong/firebase/FCMManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
@file:Suppress( | ||
"MissingFirebaseInstanceTokenRefresh", | ||
"MagicNumber", | ||
) | ||
|
||
package com.comit.simtong.firebase | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.os.Build | ||
import androidx.core.app.NotificationCompat | ||
import com.comit.simtong.R | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
|
||
class FCMManager : FirebaseMessagingService() { | ||
|
||
private companion object Channel { | ||
const val CHANNEL_ID = "simtong_fcm" | ||
const val CHANNEL_NAME = "SimTong" | ||
} | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
super.onMessageReceived(remoteMessage) | ||
if (remoteMessage.data.isNotEmpty()) { | ||
sendNotification(remoteMessage) | ||
} | ||
} | ||
|
||
private fun sendNotification(remoteMessage: RemoteMessage) { | ||
val title = remoteMessage.data["title"] | ||
val message = remoteMessage.data["message"] | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val notificationChannel = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
val channelMessage = NotificationChannel( | ||
CHANNEL_ID, CHANNEL_NAME, | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
|
||
channelMessage.apply { | ||
enableLights(true) | ||
enableVibration(true) | ||
setShowBadge(false) | ||
vibrationPattern = longArrayOf(100, 200, 100, 200) | ||
} | ||
notificationChannel.createNotificationChannel(channelMessage) | ||
|
||
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID) | ||
.setSmallIcon(R.drawable.ic_launcher_background) | ||
.setContentTitle(title) | ||
.setContentText(message) | ||
.setChannelId(CHANNEL_ID) | ||
.setAutoCancel(true) | ||
.setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE) | ||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
notificationManager.notify(9999, notificationBuilder.build()) | ||
} else { | ||
val notificationBuilder = NotificationCompat.Builder(this, "") | ||
.setSmallIcon(R.drawable.ic_launcher_background) | ||
.setContentTitle(title) | ||
.setContentText(message) | ||
.setAutoCancel(true) | ||
.setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE) | ||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.notify(9999, notificationBuilder.build()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.comit.simtong.firebase | ||
|
||
import com.comit.domain.exception.UnknownException | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
|
||
/** | ||
* FCM 에서 사용되는 DeviceToken 을 불러오는 함수 | ||
* | ||
* @return 디바이스 토큰 | ||
* @throws UnknownException DeviceToken 을 불러오지 못한 경우 발생 | ||
*/ | ||
internal fun getDeviceToken(): String = | ||
FirebaseMessaging.getInstance().token.result | ||
?: throw UnknownException("디바이스 토큰 발급에 실패했습니다.\n잠시 후 다시 시도해주세요.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.comit.simtong.root | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.comit.domain.usecase.users.SaveDeviceTokenUseCase | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
/** | ||
* TODO(limsaehyun) | ||
* | ||
* Device 토큰을 google-service 가 위치한 app 에서 추출하여 저장하기 위해 | ||
* MainViewModel 이 존재함 | ||
* app 모듈에 viewModel 이 존재해도 될까? app 모듈의 범위에 대해서 고민한 필요가 있음 | ||
*/ | ||
@HiltViewModel | ||
class MainViewModel @Inject constructor( | ||
private val saveDeviceTokenUseCase: SaveDeviceTokenUseCase, | ||
) : ViewModel() { | ||
|
||
fun saveDeviceToken() { | ||
FirebaseMessaging.getInstance().token.addOnCompleteListener { task -> | ||
val token = task.result | ||
viewModelScope.launch { | ||
saveDeviceTokenUseCase( | ||
token = token | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
domain/src/main/java/com/comit/domain/usecase/users/ClearTokenUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.comit.domain.usecase.users | ||
|
||
import com.comit.domain.repository.AuthRepository | ||
import javax.inject.Inject | ||
|
||
class ClearTokenUseCase @Inject constructor( | ||
private val authRepository: AuthRepository, | ||
) { | ||
|
||
suspend operator fun invoke() = kotlin.runCatching { | ||
authRepository.clearToken() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
domain/src/main/java/com/comit/domain/usecase/users/SaveDeviceTokenUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.comit.domain.usecase.users | ||
|
||
import com.comit.domain.repository.AuthRepository | ||
import javax.inject.Inject | ||
|
||
class SaveDeviceTokenUseCase @Inject constructor( | ||
private val repository: AuthRepository, | ||
) { | ||
|
||
suspend operator fun invoke(token: String) = kotlin.runCatching { | ||
repository.saveDeviceToken( | ||
token = token, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.