gcm
Posted by Albert 2734Day 9Hour 55Min 46Sec ago [2017-10-24]
http://wiki.intellicode.co.kr/doku.php?id=%EA%B0%9C%EB%B0%9C:gcm:implementing_gcm_client
Google Play Service 설치
1. Android SDK Manager를 실행하여 Google Play services를 설치한다. 다운받은 파일은 {sdk}/extras/google/google_play_services/libproject/google-play-services_lib/libs 라는 디렉토리 안에 존재하게 된다.
※ 인터넷에서 GCM 구현에 관련된 글들을 찾아보면 대부분 gcm.jar를 사용하는데 gcm.jar 역시 deprecate 되었다. 그래서 위에서 다운받은 gcm-client-deprecated를 살펴보면 dist 디렉토리 안에 gcm.jar가 포함되어 있는 것을 확인할 수 있다. 하지만 Google에서는 Cloud Messaging Service는 모두 Google Play Service 로 통합되었기 때문에 gcm.jar를 사용하는 포스팅을 참조하기 보다는 Google Play Service SDK를 사용한 예제를 참조하는것이 좋다. Google Play Service SDK는 기본적으로 android SDK를 다운 받는다고 설치되는 것이 아니라 extra 로 따로 다운 받아야 한다. android SDK Manager를 열어서 Google Play Service SDK를 다운로드한다.
2. 이클립스에서 {sdk}/extras/google/google_play_services/libproject/google-play-services_lib 를 import 한다.
3. 내가 만든 프로젝트 - properties - Android - Library - Add… - google-play-services_lib 선택 - ok 클릭
4. lib 추가가 완료된 것을 확인할 수 있다.
Android Application 생성
1. 이클립스에서 Android Application Project를 생성한다. 이 문서에서 패키지명은 com.intellicode.gcm_test를 사용한다.
2. Google Play Service를 사용하기 위해서는 AndroidManifest.xml 파일 안의 <application>태그 안에 <meta-data> 값으로 Google Play Service 버전 정보를
넣어줘야하는데 이 정보는 /안드로이드sdk 설치폴더 /sdk/extras/google_play_services/libproject/google-paly-services_lib/res/values/version.xml 에 존재한다.
이 파일을 복사해서 새로 생성한 프로젝트의 res/values 디렉토리 안에 추가한다.
<manifest package="com.example.gcm" ...> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /> <application ...> <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.gcm" /> </intent-filter> </receiver> <service android:name=".GcmIntentService" /> </application> </manifest>
3. <manifest package=”com.example.gcm”>에는 자신이 생성한 프로젝트의 package 명을 적는다.
4. 참고로 기본으로 생성한 프로젝트에 상단의 코드를 입력하여 수정하면 다음과 같은 manifest.xml이 완성된다.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.intellicode.gcm_test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.gcm" /> </intent-filter> </receiver> <service android:name=".GcmIntentService" /> <activity android:name="com.intellicode.gcm_test.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
5. 구글에서는 GCM에 관련된 클라이언트 코드를 이미 제공하고 있으니 그 코드를 사용하기로 한다.
https://code.google.com/p/gcm/에 접속하여 우측 하단에 있는 download Zip 버튼을 눌러서 전체 파일을 다운로드 한다.
6. 다운받은 압축 파일을 해제하여 /gcm-master/gcm-client/GcmClient/src/main/java/com/google/android/gcm/demo/app에서
GcmBroadcastReceiver.java, GcmIntentService.java, DemoActivity.java를 생성한 프로젝트의 src/com/intellicode/gcm_test에 복사한다.
이 때, DemoActivity.java는 MainActivity.java로 이름을 바꿔서 복사한다.
7. /gcm-master/gcm-client/GcmClient/src/main/에서 res폴더를 생성한 프로젝트의 res폴더에 복사한다.
8. 복사한 MainAcitivy.java를 열어서 String SENDER_ID = “”;에 이전에 발급받은 Project number를 입력한다.
9. MainActivity.java의 onCreate에서 regid = getRegistrationId(context); 밑에 Log.e(TAG, “RegistrationId: ”+regid);를 추가한다.
이는 서버에 기기를 등록할 때 필요한 Registration ID를 얻기 위함이다.
10. GcmIntentService.java를 열면 에러를 찾을 수 있는데 푸시가 도착하면 DemoActivity를 열려고 해서 발생하는 것이다.
DemoActivity.class를 생성한 프로젝트의 main클래스명으로 바꿔준다. 여기서는 MainActivity.class를 사용한다.
private void sendNotification(String msg) { mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, **MainActivity**.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_gcm) .setContentTitle("GCM Notification") .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); }
11. 이로써 GCM-Client가 완성되었다.