본문 바로가기
Development/Android

[android] FCM 샘플앱 만들기

by Sonagiya 2022. 2. 23.
반응형

Android 프로젝트 만들기

  • Android프로젝트를 하나 만들어주시고 패키지명(예시 : com.sonagiya.test)을 기억해 주세요

 

Firebase console 접속(https://console.firebase.google.com/)

프로젝트 만들기

  • 프로젝트 이름은 아무거나 상관없습니다.

 

Google 애널리틱스 설정

  • 단순 푸시 테스트를 위한 거라면 off~

 

 

프로젝트 만들기 선택하면...


 

Android 앱 추가

 

앱 등록

  • 선택사항은 패스하고 패키지 이름은 실제 안드로이드 프로젝트의 패키지 이름을 입력해야 합니다.

 

구성파일 다운로드

  • google-service.json을 다운로드하여 그림과 같이 Android 프로젝트 app 폴더에 추가합니다.

 

Android 프로젝트에 Firebase SDK 추가하기

  • build.gradle 에 추가
buildscript {

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }

  dependencies {
    // ...

    // Add the following line:
    classpath 'com.google.gms:google-services:4.3.10'  // Google Services plugin
  }
}

allprojects {
  // ...

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    // ...
  }
}

 

  • app/build.gradle에 추가
apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services'  // Google Services plugin

android {
  // ...
}

 

  • app/build.gradle에 종속 항목 추가
dependencies {
  // ...

  // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:29.0.4')

  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation 'com.google.firebase:firebase-messaging-ktx'
}

 

  • 설정 완료

 

Android 소스코드 추가

  • 메시지 서비스 만들기
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {
    companion object {
        val TAG = MyFirebaseMessagingService::class.java.simpleName
    }

    override fun onNewToken(p0: String) {
        super.onNewToken(p0)

        Log.d(TAG, "onNewToken $p0")
    }

    override fun onMessageReceived(p0: RemoteMessage) {
        super.onMessageReceived(p0)

        Log.d(TAG, "onMessageReceived $p0")
    }
}

 

  • AndroidManifest.xml에 서비스 추가(INTERNET 퍼미션 추가도 잊지 마세요)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sonagiya.test">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        ...

        <service
            android:name=".push.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>

 

이걸로 간단한 FCM 샘플 프로젝트를 완료했습니다. 실제 소스코드는 양이 많지 않습니다.

이외 사용할 수 있는 옵션사항이 있으니 firebase 사이트에 접속하여 문서를 읽어보시는 걸 권장드립니다.

 

다음엔 파이썬을 이용해 푸시는 보내는 예제를 작성하겠습니다.

반응형

댓글