-
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.
Merge pull request #229 from team-winey/feature/feat-fcm
[feat] FCM 푸쉬알림 / 푸쉬알림 구현
- Loading branch information
Showing
35 changed files
with
642 additions
and
14 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
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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/org/go/sopt/winey/ActivityLifecycleHandler.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,35 @@ | ||
package org.go.sopt.winey | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.os.Bundle | ||
|
||
class ActivityLifecycleHandler(private val application: Application) : | ||
Application.ActivityLifecycleCallbacks { | ||
override fun onActivityCreated(p0: Activity, p1: Bundle?) { | ||
} | ||
|
||
override fun onActivityStarted(p0: Activity) { | ||
} | ||
|
||
override fun onActivityResumed(p0: Activity) { | ||
isAppInForeground = true | ||
} | ||
|
||
override fun onActivityPaused(p0: Activity) { | ||
isAppInForeground = false | ||
} | ||
|
||
override fun onActivityStopped(p0: Activity) { | ||
} | ||
|
||
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) { | ||
} | ||
|
||
override fun onActivityDestroyed(p0: Activity) { | ||
} | ||
|
||
companion object { | ||
var isAppInForeground = false | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
app/src/main/java/org/go/sopt/winey/configuration/WineyMessagingService.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,108 @@ | ||
package org.go.sopt.winey.configuration | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.media.RingtoneManager | ||
import android.net.Uri | ||
import android.os.Build | ||
import androidx.core.app.NotificationCompat | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import org.go.sopt.winey.ActivityLifecycleHandler | ||
import org.go.sopt.winey.R | ||
import org.go.sopt.winey.domain.repository.DataStoreRepository | ||
import org.go.sopt.winey.presentation.splash.SplashActivity | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class WineyMessagingService : FirebaseMessagingService() { | ||
|
||
@Inject | ||
lateinit var dataStoreRepository: DataStoreRepository | ||
|
||
override fun onNewToken(token: String) { | ||
super.onNewToken(token) | ||
|
||
CoroutineScope(Dispatchers.IO).launch { dataStoreRepository.saveDeviceToken(token) } | ||
} | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
super.onMessageReceived(remoteMessage) | ||
if (remoteMessage.data.isNotEmpty() && !ActivityLifecycleHandler.isAppInForeground) { | ||
sendNotification(remoteMessage) | ||
} | ||
} | ||
|
||
private fun createNotificationIntent(remoteMessage: RemoteMessage): Intent { | ||
return Intent(this, SplashActivity::class.java).apply { | ||
putExtra(KEY_NOTI_TYPE, remoteMessage.data[KEY_NOTI_TYPE]) | ||
putExtra(KEY_FEED_ID, remoteMessage.data[KEY_FEED_ID]) | ||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
} | ||
} | ||
|
||
private fun createPendingIntent(intent: Intent, uniqueIdentifier: Int): PendingIntent { | ||
return PendingIntent.getActivity( | ||
this, | ||
uniqueIdentifier, | ||
intent, | ||
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE | ||
) | ||
} | ||
|
||
private fun getSoundUri(): Uri { | ||
return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | ||
} | ||
|
||
private fun generateUniqueIdentifier(): Int { | ||
return (System.currentTimeMillis() / 7).toInt() | ||
} | ||
|
||
private fun createNotificationBuilder(remoteMessage: RemoteMessage, pendingIntent: PendingIntent): NotificationCompat.Builder { | ||
val soundUri = getSoundUri() | ||
|
||
return NotificationCompat.Builder(this, CHANNEL_ID) | ||
.setSmallIcon(R.mipmap.ic_launcher) | ||
.setContentTitle(remoteMessage.data[KEY_TITLE]) | ||
.setContentText(remoteMessage.data[KEY_MESSAGE]) | ||
.setAutoCancel(true) | ||
.setSound(soundUri) | ||
.setContentIntent(pendingIntent) | ||
} | ||
|
||
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, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
notificationManager.notify(uniqueIdentifier, notificationBuilder.build()) | ||
} | ||
|
||
private fun sendNotification(remoteMessage: RemoteMessage) { | ||
val uniqueIdentifier = generateUniqueIdentifier() | ||
val intent = createNotificationIntent(remoteMessage) | ||
val pendingIntent = createPendingIntent(intent, uniqueIdentifier) | ||
val notification = createNotificationBuilder(remoteMessage, pendingIntent) | ||
|
||
showNotification(notification, uniqueIdentifier) | ||
} | ||
|
||
companion object { | ||
private const val KEY_FEED_ID = "feedId" | ||
private const val KEY_NOTI_TYPE = "notiType" | ||
private const val KEY_TITLE = "title" | ||
private const val KEY_MESSAGE = "message" | ||
private const val CHANNEL_NAME = "Notice" | ||
private const val CHANNEL_ID = "channel" | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
...in/java/org/go/sopt/winey/data/model/remote/request/RequestPatchAllowedNotificationDto.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,10 @@ | ||
package org.go.sopt.winey.data.model.remote.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestPatchAllowedNotificationDto( | ||
@SerialName("allowedPush") | ||
val allowedPush: Boolean | ||
) |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/org/go/sopt/winey/data/model/remote/request/RequestPatchFcmTokenDto.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,10 @@ | ||
package org.go.sopt.winey.data.model.remote.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestPatchFcmTokenDto( | ||
@SerialName("token") | ||
val fcmToken: String | ||
) |
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
10 changes: 10 additions & 0 deletions
10
.../java/org/go/sopt/winey/data/model/remote/response/ResponsePatchAllowedNotificationDto.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,10 @@ | ||
package org.go.sopt.winey.data.model.remote.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ResponsePatchAllowedNotificationDto( | ||
@SerialName("isAllowed") | ||
val isAllowed: Boolean | ||
) |
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
Oops, something went wrong.