implementation 'com.google.firebase:firebase-messaging-ktx'
FirebaseMessaging.getInstance().token
FirebaseMessagingService 를 사용하려면 앱 매니페스트에 다음을 추가해야 한다.
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
FirebaseMessagingService 클래스를 확장하는 service를 추가해준다.
class NotificationService : FirebaseMessagingService() {
private val TAG = "FirebaseService"
override fun onNewToken(token: String) {
Log.d(TAG, "new Token: $token")
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if(remoteMessage.data.isNotEmpty()){
sendNotification(remoteMessage)
}else {
Log.e(TAG, "data가 비어있습니다. 메시지를 수신하지 못했습니다.")
}
}
private fun sendNotification(remoteMessage: RemoteMessage) {
val uniId: Int = (System.currentTimeMillis() / 7).toInt()
val intent = Intent(this, PromiseSettingActivity::class.java)
for(key in remoteMessage.data.keys){
intent.putExtra(key, remoteMessage.data.getValue(key))
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) // Activity Stack 을 경로만 남김(A-B-C-D-B => A-B)
val pendingIntent = PendingIntent.getActivity(this, uniId, intent, PendingIntent.FLAG_MUTABLE)
// 알림 채널 이름
val channelId = "my_channel"
// 알림에 대한 UI 정보, 작업
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher) // 아이콘 설정
.setContentTitle(remoteMessage.data["title"].toString()) // 제목
.setContentText(remoteMessage.data["body"].toString()) // 메시지 내용
.setAutoCancel(true) // 알람클릭시 삭제여부
.setContentIntent(pendingIntent) // 알림 실행 시 Intent
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// 오레오 버전 이후에는 채널이 필요
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, "Notice", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
// 알림 생성
notificationManager.notify(uniId, notificationBuilder.build())
}
}
interface CloudMessagingService {
@Headers(
"Content-Type: application/json",
"Authorization: key=AAAAp3dHyfU:APA91bEOp3rEcmyHyhCvOtdZd3p-Rl6EJqqiNJCLKmscovQThchZwjJ6ksEt_nVHvKjOgQpcSv10dZvmQr1x_-VTKADav2Fub5l6KcqQDUc7lMsX9Q42y_KRuIm2pcL0xA7Ike0C4dBr"
) // 서버키
@POST("fcm/send")
suspend fun sendNotification(@Body notification: NotificationRequestBody)
}
data class NotificationRequestBody(
val to: String,
val data: NotificationRequestData
)
data class NotificationRequestData(
val title: String,
val body: String
)
https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-plain-text
https://firebase.google.com/docs/cloud-messaging/android/receive?authuser=0&hl=ko
https://stickode.tistory.com/335
https://minchanyoun.tistory.com/101
https://popcorn16.tistory.com/71
https://bacassf.tistory.com/166
https://github.com/HanYeop/Happy-Sharing/blob/master/app/src/main/java/com/hanyeop/happysharing/model/NotificationBody.kt
https://github.com/younminchan/kotlin-study/blob/main/FCMkotlin/app/src/main/java/com/example/fcm_kotlin/MyFirebaseMessagingService.kt
https://minchanyoun.tistory.com/99?category=1013568
https://anhana.tistory.com/7
https://www.google.com/search?q=%EC%BD%94%ED%8B%80%EB%A6%B0+fcm&rlz=1C5CHFA_enKR912KR912&oq=%EC%BD%94%ED%8B%80%EB%A6%B0+fcm&aqs=chrome..69i57.7731j0j7&sourceid=chrome&ie=UTF-8