Android FCM 적용
- FCM 프로젝트 생성 ( https://console.firebase.google.com/ )
- 프로젝트 만들기
- Cloud Messaging 선택
- Android 앱에 Firebase 추가 (디버그 서명 인증서는 선택사항)
- json 파일 다운로드 및 적용
- 설정
<project>/build.gradle
):buildscript { dependencies { // Add this line classpath 'com.google.gms:google-services:4.0.1' } }
<project>/<app-module>/build.gradle
):dependencies { // Add this line implementation 'com.google.firebase:firebase-core:16.0.1' } ... // Add to the bottom of the file apply plugin: 'com.google.gms.google-services'
- FCM Service Class 생성
- MyFcmService.class
- 메세지 받는 클래스
public class MyFcmService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "onMessageReceived, Message data payload : " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "onMessageReceived, Message Notification Body: "
+ remoteMessage.getNotification().getBody());
}
// Notification 소스 작성
}
}
- MyFcmInstanceIdService.class
- 토큰 값 얻기
public class MyFcmInstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// 얻은 토큰 값을 서버에 저장하거나 preference에 저장 소스 작성
}
}
- AndroidManifest.xml
- Test