๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[Android] FireBase ์—ฐ๋™ (2023.07.25~27) ๋ณธ๋ฌธ

full stack/์•ˆ๋“œ๋กœ์ด๋“œ

[Android] FireBase ์—ฐ๋™ (2023.07.25~27)

Shinjio 2023. 7. 26. 17:11

 

 

 

 

 

 

FireBase DataBase ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ

 

// Write a message to the database
//App์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” FireBase DataBase ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ
val database = Firebase.database

 

DataBase ๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

 

//DataBase ๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
val myRef = database.getReference("message")

 

Data ์ถ”๊ฐ€

 

myRef.push().setValue(KakaoVO(R.drawable.img2, "๋‚˜", "๋ญ๋จน์„๋žญ", "์˜ค์ „ 10:30"))

 

push : ๋žœ๋คํ•œ ๋ฌธ์ž์—ด๋กœ key๊ฐ’์„ ์„ค์ • 

 

 

๊ฐ™์€ key๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ ๋„ฃ์œผ๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐฑ์‹ ๋จ (์ถ”๊ฐ€X)

 

DataBase ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์™€์„œ ์ฝ๊ธฐ

 

myRef.addChildEventListener(ChildEvent(data, adapter))

 

addChildEventListener 

Firebase Realtime Database์—์„œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊ฐ์ง€ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋˜๋Š” ๋“ฑ์˜ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ์Œ

 

childEvent class

 

package com.sjy.ex20230720

import com.google.firebase.database.ChildEventListener
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError

class ChildEvent(var data : ArrayList<KakaoVO>, var adapter : KakaoAdapter): ChildEventListener{
    //์ƒ์„ฑ์ž๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ฐ›๊ธฐ

    override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
        //๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ๊ฐ์ง€
        //snapshot => firebase์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ
        //json๊ตฌ์กฐ๋กœ ์‘๋‹ตํ•จ => KakaoVO ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
        var temp : KakaoVO? = snapshot.getValue(KakaoVO::class.java)
        data.add(temp!!) //!! : not null ๋‹จ์ •

    }

    override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) {
        //๋ฐ์ดํ„ฐ ๋ณ€ํ™” ๊ฐ์ง€
    }

    override fun onChildRemoved(snapshot: DataSnapshot) {
        //๋ฐ์ดํ„ฐ ์‚ญ์ œ ๊ฐ์ง€
    }

    override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {
        //๋ฐ์ดํ„ฐ ์ด๋™ ๊ฐ์ง€
    }

    override fun onCancelled(error: DatabaseError) {
        //๋ฌธ์ œ ๋ฐœ์ƒ ๊ฐ์ง€
    }
}

 

 

VO

 

package com.sjy.ex20230720

data class KakaoVO(var img : Int? = 0, var name : String? = "", var msg : String? = "", var time : String? ="")
//ChildEvent ํด๋ž˜์Šค์—์„œ DataSnapshot์„ KakaoVO๋กœ ํ˜•๋ณ€ํ™˜ํ•˜๊ณ  ์žˆ์Œ
//=> getValue(KakaoVO::class.java)
//java๋ฒ„์ ผ => getValue(KakaoVO.class)
//์ค‘์š”! ํ˜•๋ณ€ํ™˜์‹œ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•จ!
//๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ž€ ? ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ์ƒ์„ฑ์ž => KakaoVO(){}
//์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž๋™์œผ๋กœ ํ•œ ๋ฒˆ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ
//Kotlin์—์„œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•! => ๋งค๊ฐœ๋ณ€์ˆ˜์— null๊ฐ’์„ ํ—ˆ์šฉํ•ด์ฃผ๋ฉด ๋จ

 

 

Volley vs FireBase

Volley 

  • ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์“ธ ๋•Œ
  • ex. ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ

 

FireBase, FCM(์•Œ๋ฆผ๋„์šฐ๊ธฐ)

  • ์‹ค์‹œ๊ฐ„ ํ†ต์‹ 
  • ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ๊ฐ€ ๊ฐ„๋‹จํ•  ๋•Œ
  • ex. ์ฑ„ํŒ