2021년 2월 5일 금요일

[Android] Firebase 도입 & 간단 테스트(analytics, fcm)

Firebase로 디비저장, 자료저장, 채팅기능, 푸쉬수신기능,분석기능 등 한다. 요즘 다한다.

https://firebase.google.com/products-build#develop-products

1.firebase 사이트에서 등록하고, 만들고 있고 있는 앱의 package이름을 입력하고, google-serivice.json 을 app폴더(현재프로젝터의  app폴더) 넣어준다.

2.Project의 build.gradle에 준비

https://github.com/sugoigroup/myexample_firebase1/commit/9132ecfa1eb3e5ebc600741afba7e73b3d545a34


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

dependencies {
...
implementation platform('com.google.firebase:firebase-bom:26.4.0')
implementation 'com.google.firebase:firebase-analytics-ktx' //자바라면 -ktx를 지운다.
}


2.Firebase에 있는 설명대로 했더니, analytics가 있으니, google analytics 를 연계한 앱분석용 로그를 firebase에 남겨보자. 일단 google analytics가 firebase에 연동되어 있어야된다고 한다. 간단하

https://github.com/sugoigroup/myexample_firebase1/commit/ea11a6b5d2fdaf5587a8c9e7a331b6aaafeb2e86

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class MainActivity : AppCompatActivity() {
    private var mFirebaseAnalytics: FirebaseAnalytics? = null

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

        val bundle = Bundle()
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "MyAPp")

        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
        mFirebaseAnalytics?.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle)
    }
}




3.Firebase FCM으로 단말기에 푸시알림창 보내보자.
메세징관련 파이어베이스라이브러리를 추가하고, 언제 메시지 받을지 모르니 안드로이드서비스로 하나 등록하고, 서비스가 메시지 받을때 해야될 클래스를 만들면 기본은 완성이다.

https://github.com/sugoigroup/myexample_firebase1/commit/b18bae3a9cf338b14a88aeeddf4404ba353ada03

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---build.gradle(Module*...)

dependencies {
....
    //push 용
    implementation 'com.google.firebase:firebase-messaging:21.0.1'

}


---AndroidManifest.xml 

    <application
        ...>
        <activity android:nam.../>
        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>

                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>

        </service>
    </application>


---MyFirbaseMessagingSavice.kt 

class MyFirebaseMessagingService : class MyFirebaseMessagingService : FirebaseMessagingService() {

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (remoteMessage.notification != null) {
            // Firebase FCM 에서 받은건 Log도 찍고
            Log.d(TAG, "From: " + remoteMessage.from)
            Log.d(TAG, "Notification Message Body: " + remoteMessage.notification!!.body)

            // 팝업은 github
        }

    }

}


4.Firebase Realdatabase에 넣어보자.

https://github.com/sugoigroup/myexample_firebase1/commit/25d7f09bcef8d20a923e4633e237703ea9d419d2


이밖에 웬만한 인증지원(전화인증까지지원), 스토어, 디비(key-value), 리얼타임 디비, 머신러닝, 버그분석, 람다형 서버함수지원, 원격 컨픽, 다이나믹링크, 애드몹 등 다양하다.

0 comments:

댓글 쓰기