[TIL] ๐ŸŒผ24/07/08๐ŸŒผ#FCM #Firebase Cloud Messaging(1)

0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
104/104
post-thumbnail
post-custom-banner

[TIL] ๐ŸŒผ24/07/08๐ŸŒผ

  • ์ง€๋‚œ ํ•œ ๋‹ฌ๊ฐ„ <๋†๋ฆผ์ถ•์‚ฐ์‹ํ’ˆ๋ถ€ ๊ณต๊ณต๋ฐ์ดํ„ฐ ํ™œ์šฉ ์ฐฝ์—…๊ฒฝ์ง„๋Œ€ํšŒ ๊ณต๋ชจ์ „>์„ ์ค€๋น„ํ–ˆ๋‹ค.
    ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ ์ค‘์—๋Š” TIL์ด ๋Š˜ ์šฐ์„ ์ˆœ์œ„์—์„œ ๋ฐ€๋ ค๋‚˜๊ฒŒ ๋œ๋‹ค.
    (๋‚ด๋ฐฐ์บ  ๋•Œ๋ถ€ํ„ฐ ๊ณ ์น˜๋ ค ํ–ˆ์ง€๋งŒ ์•„์ง๋„ ๊ณ ์ณ์ง€์ง€ ์•Š์€ ๊ณ ์งˆ๋ณ‘)
  • ๋‚ด์ผ๋ถ€ํ„ฐ๋Š” <๊ด€๊ด‘๋ฐ์ดํ„ฐ ํ™œ์šฉ ๊ณต๋ชจ์ „> ์ค€๋น„๋ฅผ ์‹œ์ž‘ํ•  ์˜ˆ์ •์ด๋‹ค.
    ์ด๋ฒˆ์—๋Š” TIL์„ ๋ฐ€๋ฆฌ์ง€ ์•Š๊ณ  ์ž‘์„ฑํ•ด์•ผ์ง€. ๊ผฌ์˜ฅ...

Firebase Cloud Messaging(1)

FCM

  • FCM?
  • ์ฃผ์š” ๊ธฐ๋Šฅ
    • ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€/๋ฐ์ดํ„ฐ ๋ฉ”์„ธ์ง€ ์ „์†ก
    • ๋‹จ์ผ ๊ธฐ๊ธฐ/๊ธฐ๊ธฐ ๊ทธ๋ฃน/์ฃผ์ œ๋ฅผ ๊ตฌ๋…ํ•œ ๊ธฐ๊ธฐ์˜ ํด๋ผ์ด์–ธํŠธ ์•ฑ์— ๋ฉ”์„ธ์ง€ ๋ฐฐํฌ ๊ฐ€๋Šฅ
    • ํด๋ผ์ด์–ธํŠธ ์•ฑ์—์„œ ๋ฉ”์„ธ์ง€ ์ „์†ก
  • ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€ vs ๋ฐ์ดํ„ฐ ๋ฉ”์„ธ์ง€?
    • ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€
      • FCM SDK์—์„œ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ
        • ํด๋ผ์ด์–ธํŠธ ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ ์ค‘์ผ ๋•Œ: FCM SDK๊ฐ€ ์•ฑ์„ ๋Œ€์‹ ํ•˜์—ฌ ๋ฉ”์„ธ์ง€ ํ‘œ์‹œ
        • ํด๋ผ์ด์–ธํŠธ ์•ฑ์ด ํฌ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ ์ค‘์ผ ๋•Œ: ์•ฑ ์ฝ”๋“œ์— ๋”ฐ๋ผ ๋™์ž‘ ๊ฒฐ์ •
      • ์‚ฌ์ „ ์ •์˜๋œ ํ‚ค ๋ชจ์Œ O
      • ๋ฐ์ดํ„ฐ ํŽ˜์ด๋กœ๋“œ(์ปค์Šคํ…€ ํ‚ค-๊ฐ’ ์Œ) ํฌํ•จ ๊ฐ€๋Šฅ
        -> ์•Œ๋ฆผ ํŽ˜์ด๋กœ๋“œ: FCM์ด ์ฒ˜๋ฆฌ ๋‹ด๋‹น
        -> ๋ฐ์ดํ„ฐ ํŽ˜์ด๋กœ๋“œ: ํด๋ผ์ด์–ธํŠธ ์•ฑ์ด ์ฒ˜๋ฆฌ ๋‹ด๋‹น
    • ๋ฐ์ดํ„ฐ ๋ฉ”์„ธ์ง€
      • ํด๋ผ์ด์–ธํŠธ ์•ฑ์ด ๋ฉ”์„ธ์ง€ ์ฒ˜๋ฆฌ ๋‹ด๋‹น
      • ์˜ˆ์•ฝ ํ‚ค ์ด๋ฆ„ X, ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ์ปค์Šคํ…€ ํ‚ค-๊ฐ’ ์Œ๋งŒ O

์•Œ๋ฆผ ๋ฉ”์„ธ์ง€

  • ์˜ˆ) JSON ํ˜•์‹ ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€
{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}
  • ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์ธ ๊ฒฝ์šฐ: ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€ ์•Œ๋ฆผ ๋ชฉ๋ก์œผ๋กœ ์ „์†ก๋จ
  • ์•ฑ์ด ํฌ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์ธ ๊ฒฝ์šฐ: ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋ฉ”์„ธ์ง€ ์ฒ˜๋ฆฌ
  • ์˜ˆ) JSON ํ˜•์‹ ๋ฐ์ดํ„ฐ ํŽ˜์ด๋กœ๋“œ๊ฐ€ ํฌํ•จ๋œ ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€
{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
}
  • ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์ธ ๊ฒฝ์šฐ:
    • ์•Œ๋ฆผ ํŽ˜์ด๋กœ๋“œ๊ฐ€ ์•Œ๋ฆผ ๋ชฉ๋ก์œผ๋กœ ์ „์†ก๋จ
    • ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ๋ฆผ์„ ํƒญํ•œ ๊ฒฝ์šฐ -> ์•ฑ์—์„œ ๋ฐ์ดํ„ฐ ํŽ˜์ด๋กœ๋“œ ์ฒ˜๋ฆฌ
  • ์•ฑ์ด ํฌ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์ธ ๊ฒฝ์šฐ: ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋ฉ”์„ธ์ง€ ์ฒ˜๋ฆฌ

๋ฐ์ดํ„ฐ ๋ฉ”์„ธ์ง€

  • ์˜ˆ) JSON ํ˜•์‹ ๋ฐ์ดํ„ฐ ๋ฉ”์„ธ์ง€
{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"
    }
  }
}
  • ์ปค์Šคํ…€ ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ ํŽ˜์ด๋กœ๋“œ ์ „์†ก
    • ์ •๋ณด๋Š” ๊ณตํ†ต data ํ‚ค์— ์บก์Šํ™”
    • ํด๋ผ์ด์–ธํŠธ ์•ฑ์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ์ปจํ…์ธ  ํ•ด์„
  • ๋ฐ์ดํ„ฐ ๋ฉ”์„ธ์ง€ ์•”ํ˜ธํ™”
    • Android ์ „์†ก ๋ ˆ์ด์–ด์—์„œ ์ง€์  ๊ฐ„ ์•”ํ˜ธํ™” ์‚ฌ์šฉ
    • ํ•„์š”์— ๋”ฐ๋ผ ์—”๋“œ ํˆฌ ์—”๋“œ ์•”ํ˜ธํ™” ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
      • FCM์—์„œ ์—”๋“œ ํˆฌ ์—”๋“œ ์†”๋ฃจ์…˜ ์ œ๊ณต X
      • ์™ธ๋ถ€ ์†”๋ฃจ์…˜(ex. Capillary, DTLS) ์‚ฌ์šฉ ๊ฐ€๋Šฅ

Cloud Messaging ์—ฐ๋™ํ•˜๊ธฐ

  • Firebase Console์— ํ”„๋กœ์ ํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ
  • Android Studio ์ขŒ์ธก ๋ฐฐ๋„ˆ Project ๋ทฐ๋กœ ๋ณ€๊ฒฝ -> app ํŒจํ‚ค์ง€์— google-services.json ์ถ”๊ฐ€ํ•˜๊ธฐ
  • plugin & dependency ์ถ”๊ฐ€ํ•˜๊ธฐ
// project gradle
plugins {
    id("com.google.gms.google-services") version "4.4.2" apply false
}
// module gradle
plugins {
    id("com.google.gms.google-services")
}


dependencies {
    implementation(platform(libs.firebase.bom))
    implementation(libs.firebase.analytics)
    implementation(libs.firebase.cloud.messaging)
}
// libs.versions.toml
[versions]
firebaseCloudMsg = "23.2.1"
firebaseBom = "33.1.1"

[libraries]
firebase-cloud-messaging = { module = "com.google.firebase:firebase-messaging-ktx", version.ref = "firebaseCloudMsg" }
firebase-analytics = { module = "com.google.firebase:firebase-analytics" }
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }
  • Cloud Messaging ์—ฐ๋™ ์ž˜ ๋๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
    • Firebase token ๋ฐ›์•„์™€ ์ถœ๋ ฅํ•˜๊ธฐ
    • Firebase Console์—์„œ ํ•ด๋‹น token์œผ๋กœ ํ…Œ์ŠคํŠธ ์•Œ๋žŒ ๋ณด๋‚ด๊ธฐ
    // onCreate() ๋‚ด๋ถ€์—์„œ ํ˜ธ์ถœ
    private fun initFirebase() {
        FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
            if (task.isSuccessful) tokenTextView.text = task.result
        }
    }

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€