Repository navigation
[FEAT/#90] FCM 세팅 #117
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
Merged
Merged
[FEAT/#90] FCM 세팅 #117
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c75bbb9
[chore/#90] gitignore 수정
vahkjsdf 7a265b3
[merge/#90] pulled develop
vahkjsdf e72881e
[add/#90] google-services, firebase SDK 라이브러리 및 의존성 추가
vahkjsdf 578f161
[feat/#90] 알림 권한 설정
vahkjsdf 167c29c
[feat/#90] 알림 권한 혀용 여부 설정
vahkjsdf 5bfd29e
[feat/#90] 토큰 자동 초기화 방지
vahkjsdf ec39cf1
[feat/#90] fcm 토큰 및 디바이스 id 불러오기
vahkjsdf 83992fc
[feat/#90] 알림 설정
vahkjsdf f495ce9
[feat/#90] 알림 권한 및 모임 상세 이동 구현
vahkjsdf 5eb5d91
[fix/#90] pr_checker 수정
vahkjsdf 0cc30d7
[mod/#90] meetingId - onSaveInstanceState로 저장
vahkjsdf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ | |
| .externalNativeBuild | ||
| .cxx | ||
| local.properties | ||
| google-services.json | ||
This file contains hidden or 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 hidden or 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
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/moive/app/core/fcm/FirebaseMessagingManager.kt
This file contains hidden or 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,49 @@ | ||
| package com.moive.app.core.fcm | ||
|
|
||
| import com.google.firebase.installations.FirebaseInstallations | ||
| import com.google.firebase.messaging.FirebaseMessaging | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import timber.log.Timber | ||
| import javax.inject.Inject | ||
| import kotlin.coroutines.resume | ||
|
|
||
| class FirebaseMessagingManager @Inject constructor() { | ||
|
|
||
| //서버 연결 | ||
| suspend fun getFcmToken(): String? = suspendCancellableCoroutine { continuation -> | ||
| FirebaseMessaging.getInstance().token.addOnCompleteListener { task -> | ||
| if (continuation.isActive) { | ||
| if (!task.isSuccessful) { | ||
| Timber.tag(TAG).e("FCM Token 불러오기 실패했습니다. ${task.exception}") | ||
| continuation.resume(null) | ||
| return@addOnCompleteListener | ||
| } | ||
|
|
||
| val token = task.result | ||
| Timber.tag(TAG).d("토큰 : $token") | ||
|
vahkjsdf marked this conversation as resolved.
|
||
| continuation.resume(token) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //디바이스 ID | ||
| suspend fun getInstallationId(): String? = suspendCancellableCoroutine { continuation -> | ||
| FirebaseInstallations.getInstance().id.addOnCompleteListener { task -> | ||
| if (continuation.isActive) { | ||
| if (!task.isSuccessful) { | ||
| Timber.tag(TAG).e("FireBaseInstallations id 불러오기 실패했습니다 : ${task.exception}") | ||
| continuation.resume(null) | ||
| return@addOnCompleteListener | ||
| } | ||
|
|
||
| val installationId = task.result | ||
| Timber.tag(TAG).d("device ID : $installationId") | ||
| continuation.resume(installationId) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "FCM" | ||
| } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
app/src/main/java/com/moive/app/core/fcm/MoiveFirebaseMessagingService.kt
This file contains hidden or 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,98 @@ | ||
| package com.moive.app.core.fcm | ||
|
|
||
| import android.Manifest | ||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager | ||
| import android.app.PendingIntent | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import android.os.Build | ||
| import androidx.core.app.NotificationCompat | ||
| import androidx.core.app.NotificationManagerCompat | ||
| import androidx.core.content.ContextCompat | ||
| import com.google.firebase.messaging.FirebaseMessagingService | ||
| import com.google.firebase.messaging.RemoteMessage | ||
| import com.moive.app.R | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.cancel | ||
| import timber.log.Timber | ||
|
|
||
| class MoiveFirebaseMessagingService: FirebaseMessagingService() { | ||
|
|
||
| private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) | ||
|
|
||
| override fun onNewToken(token: String) { | ||
| super.onNewToken(token) | ||
|
|
||
| Timber.tag(TAG).d("token: $token") | ||
| //Todo: 서버 토큰 전달 | ||
|
vahkjsdf marked this conversation as resolved.
|
||
| } | ||
|
|
||
| override fun onDestroy() { | ||
| serviceScope.cancel() | ||
| super.onDestroy() | ||
| } | ||
|
|
||
| override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
| super.onMessageReceived(remoteMessage) | ||
|
|
||
| val title = remoteMessage.data[MESSAGE_TITLE] ?: return | ||
| val body = remoteMessage.data[MESSAGE_BODY] | ||
| val meetingId = remoteMessage.data[MESSAGE_MEETING_ID]?.toLongOrNull() | ||
|
|
||
| showNotification(title, body, meetingId) | ||
| } | ||
|
|
||
| private fun showNotification(title: String, body: String?, meetingId: Long?) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| val channel = NotificationChannel( | ||
| CHANNEL_ID, | ||
| CHANNEL_NAME, | ||
| NotificationManager.IMPORTANCE_HIGH, | ||
| ).apply { | ||
| enableVibration(true) | ||
| } | ||
| getSystemService(NotificationManager::class.java).createNotificationChannel(channel) | ||
| } | ||
|
|
||
| val granted = ContextCompat.checkSelfPermission( | ||
| this, | ||
| Manifest.permission.POST_NOTIFICATIONS, | ||
| ) == PackageManager.PERMISSION_GRANTED | ||
| if (!granted) return | ||
|
|
||
| val notificationId = System.currentTimeMillis().toInt() | ||
|
|
||
| val contentIntent = packageManager.getLaunchIntentForPackage(packageName)?.apply { | ||
| flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP | ||
| meetingId?.let { putExtra(MESSAGE_MEETING_ID, it) } | ||
| } ?: return | ||
| val pendingIntent = PendingIntent.getActivity( | ||
| this, | ||
| notificationId, | ||
| contentIntent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, | ||
| ) | ||
|
|
||
| val notification = NotificationCompat.Builder(this, CHANNEL_ID) | ||
| .setSmallIcon(R.drawable.img_logo_white_bg) | ||
| .setContentTitle(title) | ||
| .setContentText(body) | ||
| .setAutoCancel(true) | ||
| .setContentIntent(pendingIntent) | ||
| .build() | ||
|
|
||
| NotificationManagerCompat.from(this).notify(notificationId, notification) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "FCM" | ||
| const val MESSAGE_MEETING_ID = "meetingId" | ||
| private const val MESSAGE_TITLE = "title" | ||
| private const val MESSAGE_BODY = "body" | ||
| private const val CHANNEL_ID = "moive_default_channel" | ||
| private const val CHANNEL_NAME = "일반 알림" | ||
| } | ||
| } | ||
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/moive/app/presentation/notification/NotificationPermissionManager.kt
This file contains hidden or 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,40 @@ | ||
| package com.moive.app.presentation.notification | ||
|
|
||
| import android.Manifest | ||
| import android.content.pm.PackageManager | ||
| import android.os.Build | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.core.content.ContextCompat | ||
| import timber.log.Timber | ||
|
|
||
| class NotificationPermissionManager( | ||
| private val activity: ComponentActivity | ||
| ) { | ||
|
|
||
| private val requestPermissionLauncher = activity.registerForActivityResult( | ||
| ActivityResultContracts.RequestPermission(), | ||
| ) { isGranted -> | ||
| if (!isGranted) { | ||
| Timber.tag(TAG).d(NOTIFICATION_MESSAGE) | ||
| } | ||
| } | ||
|
|
||
| fun askNotificationPermission() { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return | ||
|
|
||
| val granted = ContextCompat.checkSelfPermission( | ||
| activity, | ||
| Manifest.permission.POST_NOTIFICATIONS, | ||
| ) == PackageManager.PERMISSION_GRANTED | ||
|
|
||
| if (!granted) { | ||
| requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "Notification" | ||
| private const val NOTIFICATION_MESSAGE = "알림을 표시할 수 없습니다." | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.