-
Notifications
You must be signed in to change notification settings - Fork 30
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
부산대 Android_이창욱_6주차_과제_step2 #56
Open
ichanguk
wants to merge
8
commits into
kakao-tech-campus-2nd-step2:ichanguk
Choose a base branch
from
ichanguk:step2
base: ichanguk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0ccd16b
feat: add Splash Screen
ichanguk 6da1bbb
docs: update README.md
ichanguk cad4ed5
feat: update Navigate to Map Activity only if ServiceState is ON_SERV…
ichanguk 523b85b
Update README.md
ichanguk 99bc3dd
refactor: apply MVVM pattern to Splash Activity
ichanguk 6249dca
refactor: remove SplashScreen's Delay
ichanguk 90f37c5
feat: update FCM Notification feature
ichanguk df459da
docs: update README.md
ichanguk 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 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
86 changes: 86 additions & 0 deletions
86
app/src/main/java/campus/tech/kakao/map/data/network/service/MapFirebaseMessagingService.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,86 @@ | ||
package campus.tech.kakao.map.data.network.service | ||
|
||
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 android.util.Log | ||
import androidx.core.app.NotificationCompat | ||
import campus.tech.kakao.map.R | ||
import campus.tech.kakao.map.ui.splash.SplashActivity | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
|
||
class MapFirebaseMessagingService : FirebaseMessagingService() { | ||
private lateinit var notificationManager: NotificationManager | ||
private val TAG = "FirebaseService" | ||
|
||
override fun onNewToken(token: String) { | ||
Log.d(TAG, "Refreshed token: $token") | ||
} | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
sendNotification(remoteMessage) | ||
} | ||
|
||
private fun sendNotification(remoteMessage: RemoteMessage) { | ||
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
createNotificationChannel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. notification channel 은 매번 처리하기보다, 한번만 처리하는 편이 좋을 것 같네요! |
||
val pendingIntent = createPendingIntent() | ||
val notification = buildNotification(remoteMessage, pendingIntent) | ||
|
||
notificationManager.notify(NOTIFICATION_ID, notification) | ||
} | ||
|
||
private fun buildNotification(remoteMessage: RemoteMessage, pendingIntent: PendingIntent): Notification { | ||
return NotificationCompat.Builder( | ||
this, | ||
CHANNEL_ID, | ||
) | ||
.setSmallIcon(R.drawable.ic_kakaomap) | ||
.setContentTitle("[포그라운드 알림] ${remoteMessage.notification?.body}") | ||
.setContentText(getString(R.string.notification_content)) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
.setContentIntent(pendingIntent) | ||
.setStyle( | ||
NotificationCompat.BigTextStyle() | ||
.bigText(getString(R.string.notification_big_text)), | ||
) | ||
.setAutoCancel(true) | ||
.build() | ||
} | ||
|
||
private fun createPendingIntent(): PendingIntent { | ||
val intent = Intent(this, SplashActivity::class.java).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
val descriptionText = getString(R.string.fcm_channel_description) | ||
val channel = NotificationChannel( | ||
CHANNEL_ID, | ||
CHANNEL_NAME, | ||
NotificationManager.IMPORTANCE_DEFAULT, | ||
).apply { | ||
description = descriptionText | ||
} | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
fun getFirebaseToken() { | ||
FirebaseMessaging.getInstance().token.addOnSuccessListener { | ||
Log.d(TAG, "token=$it") | ||
} | ||
} | ||
Comment on lines
+75
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하지 않는 코드인것 같네요! |
||
|
||
companion object { | ||
private const val NOTIFICATION_ID = 222222 | ||
private const val CHANNEL_ID = "main_default_channel" | ||
private const val CHANNEL_NAME = "main channelName" | ||
} | ||
} |
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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onNewToken 은 갱신된 토큰이 내려오는 편이에요. 보통 data store 등에 저장해두었다가 서버에 보내는 작업을 진행하기도 합니다.