본문 바로가기
개발/안드로이드

FCM firebase

by 파란검정 2016. 6. 15.
반응형

안드로이드 포스팅을 시작하기로 하면서 가장 먼저 쓰는 글이 GCM을 대신할 FCM이다. 


2014년 firebase를 구글이 인수했다. 이후에 이렇게 준비해서 내어 놓을 줄이야.. 


간략하게 어떻게 사용하는지 보자 



android studio를 기준으로 작성되었다. 


잡설이지만..


나는 기본적으로 이클립스로 안드로이드 개발을 해오고 있었다. 


android studio를 이용해보려고 몇번씩이나 시도해보다가 바쁜 프로젝트 일정 및 사양 낮은 pc환경으로 인해


핑계아닌 핑계를 대고 미뤄 두고 있었다. 


android sdk 매니저를 통해 google play service를 업그레이드 하고 났더니 gcm이 동작하지 않는 것이었다. 


이것을 이유로 FCM을 접해 보고 간략하게 정리해 본다. 




우선 다음 링크를 보고 참고 해보자.


//*************************** 사전 참고 


1 project 세팅 - 
https://firebase.google.com/docs/android/setup 

2 regid 준비 - https://firebase.google.com/docs/cloud-messaging/android/client 

3 push notification 
https://firebase.google.com/docs/cloud-messaging/downstream#sample-receive 



//************************************* 실제 적용 

1. project에 dependency를 추가 해준다. 
project level - gradle에 다음을 참고 하여 classpath를 추가해 준다. 

buildscript { 
// ... 
dependencies { 
// ... 
classpath 'com.google.gms:google-services:3.0.0' 


app level - gradle에 다음을 참고 하여 compile을 추가해 준다. 

맨밑에 추가 맨 밑줄에 apply plugin을 추가 해준다. 
dependencies { 
// ... 
compile 'com.google.firebase:firebase-core:9.0.2' 



// ADD THIS AT THE BOTTOM 
apply plugin: 'com.google.gms.google-services' 




2. manifest에 서비스 추가 

manifest에 서비스를 추가해 준다. 두 서비스는 firebase의 서비스클래스를 상속받은 클래스이다.  

작성자에 따라 달라 질수 있다. intent-filter에 더 주의 하자

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

... 

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




소스 level에서의 구현 

FirebaseInstanceIdService를 상속받은 class에서 

Token 취득 부분 구현 (push에 사용하는 token이다)

@Override 
public void onTokenRefresh() { 
// Get updated InstanceID token. 
String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
Log.d(TAG, "Refreshed token: " + refreshedToken); 

// TODO: Implement this method to send any registration to your app's servers. 
sendRegistrationToServer(refreshedToken); 



FirebaseMessagingService를 상속받은 class에서 

message 취득 부분 구현 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
// TODO(developer): Handle FCM messages here. 
// If the application is in the foreground handle both data and notification messages here. 
// 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. 
Log.d(TAG, "From: " + remoteMessage.getFrom()); 
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 



마지막으로 

firebase 에서 project를 만들면 나오는 json파일을 프로젝트에 넣어 주면 client에서의 준비는 끝이 난다. 



push테스트는 위 코드를 넣어 실행한 후 나온 token을 이용하여 2가지 방법으로 보낼 수 있다. 


##프로젝트 콘솔 
https://console.firebase.google.com/project 

notifications 메뉴에서 push 보내기 

또는 

##api로 보내기 
https://fcm.googleapis.com/fcm/send 
Content-Type:application/json 
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA 


"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
"data" : { 
... 
}, 


네이버에 올린글보다 설명은 조금 더 있지만 화면 캡쳐가 없다보니.. 많이 부족해 보인다. 시간되면 조금 더 정리해야 겠다. 

반응형