firebase๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ธ์‹œ ์•Œ๋ฆผ ๋ฐ›๊ธฐ

2021. 8. 9. 21:15

1. ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

https://console.firebase.google.com/ ์—์„œ ํ”„๋กœ์ ํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

๋กœ๊ทธ์ธ - Google ๊ณ„์ •

ํ•˜๋‚˜์˜ ๊ณ„์ •์œผ๋กœ ๋ชจ๋“  Google ์„œ๋น„์Šค๋ฅผ Google ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธ

accounts.google.com

 

ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ์ž…๋ ฅ > ๊ณ„์† > ๊ณ„์†

 

Default Accout for Firebase ์„ ํƒ > ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

 

 

2. ์•ฑ์— ์ถ”๊ฐ€ํ•˜๊ธฐ

์•ˆ๋“œ๋กœ์ด๋“œ ํด๋ฆญ

 

์—ฐ๊ฒฐํ•˜๋ ค๋Š” android ์•ฑ ํŒจํ‚ค์ง€ ์ด๋ฆ„ ์ž…๋ ฅ > ์•ฑ ๋“ฑ๋ก

๐Ÿ’ฅ ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ๋ชจ๋ฅผ ๊ฒฝ์šฐ, app ์ˆ˜์ค€ build.gradle ํŒŒ์ผ์˜ defaultConfig -> applicationId ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

 

google-services.json ํŒŒ์ผ์„ ๋‹ค์šด๋ฐ›์€ ํ›„ ํ”„๋กœ์ ํŠธ์˜ app ํด๋”์— ๋„ฃ๊ธฐ > ๋‹ค์Œ

๐Ÿ’ฅ github ๋“ฑ์— pushํ•  ๋•Œ google-services.json ํŒŒ์ผ์€ ๋นผ๊ณ  pushํ•  ๊ฒƒ

 

project ์ˆ˜์ค€ build.gradle์— ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€

classpath 'com.google.gms:google-services:4.3.8'

 

app ์ˆ˜์ค€ build.gradle์— ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€

apply plugin: 'com.google.gms.google-services'

implementation platform('com.google.firebase:firebase-bom:28.3.0')
implementation 'com.google.firebase:firebase-analytics-ktx'

clouding messaging๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€

implementation 'com.google.firebase:firebase-messaging-ktx'

 

๋ชจ๋‘ ์ถ”๊ฐ€ ํ›„ ๋‹ค์Œ > ์ฝ˜์†”๋กœ ์ด๋™

 

3. ํ‘ธ์‹œ ์•Œ๋ฆผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ

xml์— TextView ๋งŒ๋“  ํ›„ kotlin ํŒŒ์ผ์—์„œ FirebaseMessaging๊ณผ ์—ฐ๊ฒฐ

<TextView
        android:id="@+id/firebaseTokenTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />
    private val firebaseToken: TextView by lazy {
        findViewById(R.id.firebaseTokenTextView)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFirebase()
    }

    private fun initFirebase() {
        FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
            if(task.isSuccessful) {
                val token = task.result
                firebaseToken.text = task.result
            }
        }
    }

์• ๋ฎฌ๋ ˆ์ดํ„ฐ ๋˜๋Š” ์Šค๋งˆํŠธํฐ์— ๋œฌ ํ† ํฐ ๋ณต์‚ฌํ•˜๊ธฐ

 

์ขŒ์ธก์— Cloud Messaging ํด๋ฆญ

 

Send your first message ํด๋ฆญ

 

์•Œ๋ฆผ ์ œ๋ชฉ๊ณผ ์•Œ๋ฆผ ํ…์ŠคํŠธ ์ž…๋ ฅ ํ›„ ํ…Œ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์ „์†ก ํด๋ฆญ

 

๋ณต์‚ฌํ•ด๋‘” ํ† ํฐ์„ 'FCM ๋“ฑ๋ก ํ† ํฐ ์ถ”๊ฐ€' ์ฐฝ์— ์ž…๋ ฅํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๊ณ  ํ…Œ์ŠคํŠธ

๐Ÿ’ฅ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „ ์• ๋ฎฌ๋ ˆ์ดํ„ฐ ๋˜๋Š” ์Šค๋งˆํŠธํฐ์—์„œ ํ•ด๋‹น์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์žˆ๋„๋ก ์•ฑ ์ข…๋ฃŒ ํ›„ ํ…Œ์ŠคํŠธ ์‹คํ–‰

728x90

BELATED ARTICLES

more