244일차 - 커뮤니티 앱개발 (12) : 푸시 알림
2021. 8. 30. 22:25ㆍDiary/201~300
1. App Build Gradle
plugins {
id 'com.google.gms.google-services'
}
dependencies {
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-messaging-ktx:22.0.0'
implementation 'com.google.firebase:firebase-analytics'
implementation platform('com.google.firebase:firebase-bom:28.0.1'
}
2. Project Build Gradle
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
3. FcmService.kt
class FcmService: FirebaseMessagingService()
{
override fun onMessageReceived(remoteMessage: RemoteMessage)
{
super.onMessageReceived(remoteMessage)
if (remoteMessage.notification != null)
{
sendNotification(remoteMessage.notification?.title, remoteMessage.notification!!.body!!)
}
}
override fun onNewToken(token: String)
{
super.onNewToken(token)
}
private fun sendNotification(title: String?, body: String)
{
val intent = Intent(this, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = "daily_mbti_channel"
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"o_daily_mbti_channel",
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0, notificationBuilder.build())
}
}
4. LoginActivity
// FCM
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
return@OnCompleteListener
}
fcm_token = task.result.toString()
Log.d("TEST", "Fcm Token : $fcm_token")
})
'Diary > 201~300' 카테고리의 다른 글
245일차 - 커뮤니티 앱개발 (13) : 광고 배너 (0) | 2021.08.31 |
---|---|
남은기능 정리 (0) | 2021.08.31 |
243일차 - 커뮤니티 앱개발 (11) : 게시글 & 댓글 수정, 삭제 (0) | 2021.08.29 |
242일차 - 커뮤니티 앱개발 (10) : 프로필 수정 (0) | 2021.08.28 |
240일차 - 커뮤니티 앱개발 (8) : 코틀린 회원가입, 로그인 (0) | 2021.08.26 |