GCM 앱 개발하기
Manifest 등록
- com.google.android.c2dm.permission.RECEIVE permission 등록
: 메세지 수신 및 ID 등록가능.
- android.permission.INTERNET permission 등록
: registration ID 를 3rd party server 에 보낼 수 있다.
- android.permission.GET_ACCOUNTS permission 등록
: GCM 이 Google account 를 가져갈 수 있도록 한다. ( Android 4.0.4 이하일때만.. )
- android.permission.WAKE_LOCK permission 등록
: 메세지 받았을 때 단말을 깨울 수 있게 한다.
- appPackage.permission.C2D_MESSAGE permission 등록
: 다른 앱들이 해당 message 를 받지 못하도록.
- com.google.android.c2dm.intent.RECEIVE와 com.doogle.android.c2dm.intent.REGISTRATION 을 받을 receiver 등록. 카테고리는 appPackage 로 지정되어야 한다. 해당 receiver 는 com.google.android.c2dm.SEND permission 이 설정되어야 한다.
- message 를 처리할 intent service 를 만든다.
- android:minSdkVersion="8" 명시 ( GCM 은 Android 2.2 이상에서 지원 )
<manifest package="com.example.gcm" ...>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>
<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=".MyBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service android:name=".MyIntentService" />
</application>
</manifest>
App 을 GCM Server 에 등록하기
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
// sets the app name in the intent
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", senderID);
startService(registrationIntent);
등록결과는 com.google.android.c2dm.intent.REGISTRATION intent 로 return 받는다. extra 에 registratino ID 가 포함되어 있다.
App 을 GCM Server 에서 등록해제하기
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
startService(unregIntent);
GCM 서버에서 전달된 intent 처리하기
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public final void onReceive(Context context, Intent intent) {
MyIntentService.runIntentInService(context, intent);
setResult(Activity.RESULT_OK, null, null);
}
}
public class MyIntentService extends IntentService {
private static PowerManager.WakeLock sWakeLock;
private static final Object LOCK = MyIntentService.class;
static void runIntentInService(Context context, Intent intent) {
synchronized(LOCK) {
if (sWakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock");
}
}
sWakeLock.acquire();
intent.setClassName(context, MyIntentService.class.getName());
context.startService(intent);
}
@Override
public final void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(intent);
} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(intent);
}
} finally {
synchronized(LOCK) {
sWakeLock.release();
}
}
}
}
GCM Server 등록 결과 처리하기
com.google.android.c2dm.intent.REGISTRATION intent 에는 registration_id, error, unregistered 의 3가지 extra 가 있다.
- registration_id 가 있고, 나머지가 없는 경우 3rd-party server 에 이 ID 를 등록해줘야 한다. 이는 register 를 명시적으로 call 하지 않아도, google 이 registration ID 를 바꿀 경우에도 불릴 수 있다.
- unregistered 가 있는 경우는, 3rd-party server 에 unregister 를 해줘야 한다.
- error 코드는 다음과 같다.
SERVICE_NOT_AVAILABLE : back-off 를 두고, 다시 시도해야 한다.
ACCOUNT_MISSING : phone 에 google account 가 없는 경우
AUTHENTICATION_FAILED : Google account 비밀번호가 다른 경우
INVALID_SENDER : sender account 가 인식 안 될 경우. sender extra 를 제대로 설정해야 한다.
PHONE_REGISTRATION_ERROR : GCM 을 지원하지 않는 폰일 경우
INVALID_PARAMETERS : parameter 가 부족할 경우 혹은 GCM 을 지원하지 않는 경우.
private void handleRegistration(Intent intent) {
String registrationId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
// registration succeeded
if (registrationId != null) {
// store registration ID on shared preferences
// notify 3rd-party server about the registered ID
}
// unregistration succeeded
if (unregistered != null) {
// get old registration ID from shared preferences
// notify 3rd-party server about the unregistered ID
}
// last operation (registration or unregistration) returned an
error;
if (error != null) {
if ("SERVICE_NOT_AVAILABLE".equals(error)) {
// optionally retry using exponential back-off
// (see Advanced Topics)
} else {
// Unrecoverable error, log it
Log.i(TAG, "Received error: " + error);
}
}
}
'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[Java] iterator 와 listiterator 의 차이. (0) | 2012.07.10 |
---|---|
[Android/안드로이드] GCM 서버 개발하기 ( Google Cloud Messaging service server development ) (6) | 2012.07.04 |
[Android/안드로이드] GCM (Google Cloud Messaging service ) Framework 에 대해 알아본다. (0) | 2012.07.04 |
[Android/안드로이드] GCM ( Google Cloud Messaging Service ) 에 대해 알아본다. (20) | 2012.07.04 |
[Android/안드로이드] gcm.jar 를 어디서 찾을 수 있을까? (0) | 2012.07.03 |
댓글