-
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.
Browse files
Browse the repository at this point in the history
#295 [feat] 푸시알림 구현
- Loading branch information
Showing
13 changed files
with
134 additions
and
10 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
68 changes: 68 additions & 0 deletions
68
app/src/main/java/com/sopt/peekabookaos/PeekaMessageService.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,68 @@ | ||
package com.sopt.peekabookaos | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
import com.sopt.peekabookaos.presentation.splash.SplashActivity | ||
import timber.log.Timber | ||
|
||
class PeekaMessageService : FirebaseMessagingService() { | ||
override fun onNewToken(token: String) { | ||
super.onNewToken(token) | ||
Timber.d("Refreshed token: $token") | ||
} | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
super.onMessageReceived(remoteMessage) | ||
if (remoteMessage.data.isNotEmpty()) { | ||
val title = remoteMessage.data[TITLE] ?: "" | ||
val body = remoteMessage.data[BODY] ?: "" | ||
sendNotification(title, body) | ||
} | ||
} | ||
|
||
private fun sendNotification(title: String, body: String) { | ||
createNotificationChannel() | ||
val intent = Intent(this, SplashActivity::class.java).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
val pendingIntent: PendingIntent = | ||
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) | ||
val notificationBuilder = Notification.Builder(this, CHANNEL_ID) | ||
.setContentTitle(title) | ||
.setSmallIcon(R.mipmap.ic_launcher_foreground) | ||
.setContentIntent(pendingIntent) | ||
.setContentText(body) | ||
.setAutoCancel(true) | ||
|
||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()) | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
val notificationManager = | ||
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
val channel = | ||
NotificationChannel( | ||
CHANNEL_ID, | ||
CHANNEL_NAME, | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
|
||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
companion object { | ||
const val CHANNEL_ID = "peeka_channel" | ||
const val NOTIFICATION_ID = 1 | ||
const val CHANNEL_NAME = "peeka_channel_name" | ||
const val TITLE = "title" | ||
const val BODY = "body" | ||
} | ||
} |
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
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
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/sopt/peekabookaos/domain/usecase/GetFcmTokenUseCase.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,11 @@ | ||
package com.sopt.peekabookaos.domain.usecase | ||
|
||
import com.sopt.peekabookaos.domain.repository.AuthRepository | ||
import javax.inject.Inject | ||
|
||
class GetFcmTokenUseCase @Inject constructor( | ||
private val authRepository: AuthRepository | ||
) { | ||
operator fun invoke(setFcmToken: (String) -> Unit) = | ||
authRepository.getFcmToken(setFcmToken) | ||
} |
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