매년 있는 Google I/O 행사.
매년 I/O 행사에서 Android 관련 기술들이 하나씩 발표가 되고 있다.
2016년 5월 말에 있던 I/O 행사에서 발표된 Firebase Cloud Message.
Firebase는 Google의 App을 위한 통합 플랫폼 정도로 이해해 볼수 있는데 여기서 제공되는 다양한 기능중 가장 큰 것이 Cloud Message 가 아닐까 싶다.
이 글은 개인적으로 Firebase Cloud Message를 Android 환경에서 테스트한 글이다.
테스트한 결과론적인 이야기로 기존의 GCM(Google Cloud Message)와 상당히 유사하며 작성이 GCM에 비해 간단해 졌다는 느낌이다.
![](https://github.com/kkangseongyun/kkangs_share/blob/main/event_android_kotlin.jpg?raw=true)
FCM 소개
기존 Google Cloud Message(GCM) 도 계속 지원이 되기는 하지만 GCM에 비해 FCM이 발전된 기능을 제공하며 앞으로 추가되는 기능들은 FCM에 추가될 것이라고 한다. GCM을 FCM으로 업그레이드 할것을 적극 권장하고 있다. (참조 - http://googledevkr.blogspot.kr/2016/06/google-cloud-messaging-and-firebase.html)
[[출처 - https://firebase.google.com/docs/cloud-messaging]]
전체 개념은 두 단계로 나누어 살펴볼수 있다.
1. Phone의 App이 Firebase 서버에 Key를 Request한다.
2. Firebase 서버에서 key를 만들어 Phone에 전달한다. 물론 이 작업이 될려면 미리 App이 Firebase 서버에 등록되어 있어야 한다.
3. App은 받은 Key를 메시지를 받고자 하는 Application 서버에 전송한다.
4. Application 서버에서는 받은 key를 DB에 저장한다.
1. Application 서버에서 메시지를 Phone에 전달하기 위해서 DB에서 key를 획득한다.
2. Key와 전송하고자 하는 메시지를 Firebase 서버에 전달한다.
3. Firebase 서버에서는 Phone에 메시지를 전달한다.
[새 프로젝트 만들기] 클릭
적절한 이름으로 프로젝트를 만든다.
Android App, iOS App, WebApp 을 지원해 준다. 이중 Android App 버튼 클릭
App Package Name과 SHA1 지문 입력
참고로 SHA1 지문은 keytool -list -v -keystore mystore.keystore 명령으로 개발자 PC에서 획득
위와 같은 화면이 나오면서 google-services.json 파일이 다운로드가 된다. 이 파일을 안내하는 대로 프로젝트 루트 디렉토리에 카피하면 된다.
이 파일에 FCM을 이용하기 위한 FCM Console에서 발급한 다양한 정보가 포함되어 있다.
Gradle 파일 작업
gradle에 FCM을 위한 라이브러리를 정상 적용할려면 Google Repository가 27버전 이상이어야 한다.
Project build.gradle
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' classpath 'com.google.gms:google-services:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } |
App gradle.build 파일의 맨 마지막 부분에 명시
dependencies { compile 'com.android.support:appcompat-v7:23.+' compile "com.google.firebase:firebase-messaging:9.0.1" compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' } apply plugin: 'com.google.gms.google-services' |
FirebaseMessagingService 작성
FCM 을 받기 위한 서비스 등록
AndroidManifest.xml
<service android:name=".MyFirebaseMessagingService" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> |
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService { public MyFirebaseMessagingService() { } @Override public void onMessageReceived(RemoteMessage message) { String from = message.getFrom(); String notification=message.getNotification().getBody(); Log.d("kkang",from+":"+notification); } } |
FirebaseInstanceIDService 작성
AndroidManifest.xml
<service android:name=".MyFirebaseInstanceIDService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> |
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { public MyFirebaseInstanceIDService() { } @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d("kkang","token:"+refreshedToken); } } |
FirebaseMessaging.getInstance().subscribeToTopic("test"); FirebaseInstanceId.getInstance().getToken(); |
NodeJS - FCM-push로 테스트
https://www.npmjs.com/package/fcm-push
npm install fcm-push |
var FCM = require('fcm-push'); var serverKey = ''; var fcm = new FCM(serverKey); var message = { to: 'registration_token', // required collapse_key: 'your_collapse_key', data: { your_custom_data_key: 'your_custom_data_value' }, notification: { title: 'Title of your push notification', body: 'Body of your push notification' } }; fcm.send(message, function(err, response){ if (err) { console.log("Something has gone wrong!"); } else { console.log("Successfully sent with response: ", response); } }); |
![](https://github.com/kkangseongyun/kkangs_share/blob/main/event_android_kotlin.jpg?raw=true)
'Android' 카테고리의 다른 글
Android MVC, MVVM, MVP (0) | 2017.07.20 |
---|---|
Android Studio를 이용해 쉽게 SHA1 지문 확인하기 (0) | 2017.07.18 |
EditText에서 한줄 입력 처리하기 (singleLine, lines, maxLine) (0) | 2017.01.05 |
Android AVD Camera webcam (0) | 2016.11.08 |
Error : Unsupported major.minor version 52.0 (0) | 2016.06.21 |