">

FCM 알아보기



GCM을 대신해 새로운 버전인 FCM이 나왔습니다. 안드로이드 앱에서 설정해줘야 하는 부분이 대폭 줄어서 개발자 입장에서는 편리한 점이 많은 것 같아요. 서버가 개발 되지 않았더라도 푸시 메시지를 잘 받을 수 있는지 확인도 가능합니다. 

Firebase는 엔드포인트단에서 쉽게 여러 가지 서비스가 가능하도록 도와줍니다. 

Firebase에 문서가 아직 좀 덜 정리된 느낌이여서 블로그에 정리해서 올려보았습니다. 순서대로만 하시면 Firebase에 설명보다 더 빠르게 FCM을 구성할 수 있을 겁니다. 


구현하기

1. 안드로이드 스튜디오를 통해 프로젝트를 하나 생성 합니다. 

2.  앱 수준의 build.gradle 파일에 FCM 종속 항목을 추가합니다. 


3. 모듈의 Gradle 파일에 플러그인 사용 설정을 합니다. 


4. google-services.json 파일을 다운로드 받아 안드로이드 스튜디오 프로젝트 안에 추가해 줍니다. 우선 Firebase에서 새로운 프로젝트를 생성합니다. 프로젝트 생성 후에 안드로이드 아이콘을 선택하면 아래와 같은 화면을 볼 수 있습니다. 


안드로이드 스튜디오에서 생성한 프로젝트의 패키지 명을 입력하고 앱 추가 버튼을 클릭 합니다. 

그러면 또 아래와 같은 이미지를 확인할 수 있는데 여기서 google-services.json 파일을 다운 받아 이미지에 있는 것 처럼 프로젝트 폴더 안에 넣어주도록 합니다. 


5. 안드로이드 스튜디오에서 빨간 테두리에 있는 버튼을 찾아 Gradle 싱크를 맞춥니다. 


6. AndroidManifest.xml 파일에 아래 코드를 추가합니다. 

  처음 서비스는 등록 토큰 생성, 순환, 업데이트를 처리하기 위해 추가한 것이고, 푸시 메시지를 특정 기기로 전송하거나 기기 그룹을 만드는 경우에 필요합니다.

  두번 째 서비스는 백그라운드에서 앱의 알림을 수신하는 것 이외에 다른 방식으로 메시지를 처리하려는 경우에 필요합니다. 포어그라운드 앱의 알림 수신, 데이터 페이로드 수신, 업스트림 메시지 전송 등을 수행하려면 이 서비스를 확장해야 합니다. 

 

 <service

    android:name=".MyFirebaseInstanceIDService">

    <intent-filter>

        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

    </intent-filter>

 </service>


 <service

    android:name=".MyFirebaseMessagingService">

    <intent-filter>

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

    </intent-filter>

 </service>



7. 프로젝트에 'MyFirebaseInstanceIDService' 와 'MyFirebaseMessageingService' 클래스 파일을 각 각 만들어 줍니다. 그리고 상속 받는 코드를 추가해 주면 됩니다. 


*MyFirebaseInstanceIDService.class

 

 public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {


    private static final String TAG = "MyFirebaseIIDService";


    @Override

    public void onTokenRefresh() {

        // Get updated InstanceID token.

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Log.d(TAG, "Refreshed token: " + refreshedToken);



        // If you want to send messages to this application instance or

        // manage this apps subscriptions on the server side, send the

        // Instance ID token to your app server.

        sendRegistrationToServer(refreshedToken);

    }

 private void sendRegistrationToServer(String token) {

        // TODO: Implement this method to send token to your app server.

    }

 }



*MyFirebaseMessageingService.class

 

 public class MyFirebaseMessagingService extends FirebaseMessagingService {


    private static final String TAG = "MyFirebaseMsgService";

   

    @Override

    public void onMessageReceived(RemoteMessage remoteMessage) {

        // [START_EXCLUDE]

        // There are two types of messages data messages and notification messages. Data messages are handled

        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type

        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app

        // is in the foreground. When the app is in the background an automatically generated notification is displayed.

        // When the user taps on the notification they are returned to the app. Messages containing both notification

        // and data payloads are treated as notification messages. The Firebase console always sends notification

        // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options

        // [END_EXCLUDE]



        // TODO(developer): Handle FCM messages here.

        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ

        Log.d(TAG, "From: " + remoteMessage.getFrom());



        // Check if message contains a data payload.

        if (remoteMessage.getData().size() > 0) {

            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        }



        // Check if message contains a notification payload.

        if (remoteMessage.getNotification() != null) {

            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        }



        // Also if you intend on generating your own notifications as a result of a received FCM

        // message, here is where that should be initiated. See sendNotification method below.

    }

    // [END receive_message]



    /**

    * Create and show a simple notification containing the received FCM message.

    *

    * @param messageBody FCM message body received.

    */

    private void sendNotification(String messageBody) {

        Intent intent = new Intent(this, MainActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

                PendingIntent.FLAG_ONE_SHOT);



        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

                .setSmallIcon(R.drawable.ic_stat_ic_notification)

                .setContentTitle("FCM Message")

                .setContentText(messageBody)

                .setAutoCancel(true)

                .setSound(defaultSoundUri)

                .setContentIntent(pendingIntent);



        NotificationManager notificationManager =

                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);



        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    }

}



8. 앱 설정은 끝났습니다. 이제 Firebase에서 메시지를 보내서 수신이 잘 되는지 테스트 하면 됩니다. 

Firebase 콘솔로 가서 좌측 메뉴 중간 부분에 Notifications를 들어가면 메시지를 보낼 수 있습니다. 


메시지가 잘 도착하면 이로써 Application 단은 수신 준비는 끝났습니다. 

물론 토큰을 생성하거나 메시지를 처리하는 부분은 추가 구현이 필요하지만 기본적인 동작은 끝난 겁니다. 


다음은 서버를 구성해서 푸시 메시지를 전송하는 법을 알아보도록 하겠습니다. 

+ Recent posts