25 March 2012
這篇是舊版的,已經被deprecated,新版為GCM,新版的實作請看
新版的Android篇




這篇主要在說明Android端的實作
第一篇在講參數說明
第二篇在講流程說明
第三篇在講server端的實作


這篇主要會完成下列項目:
  1. 基本的manifest設定(加入receiver,permission)。
  2. 向C2DM 註冊。
  3. 接收自C2DM Sever回傳的registration id 並傳送 Registration Id 到我們的Server,以及接收Message


【1.  設定Manifest資料 】 
這步驟,分了兩個部分!
第一個就是要先加入一個receiver,
記得下面的category android:name,
裡面的value要改成自己的package name
如下:
<!-- 用來接收C2DM Server的receiver,且要有send的permssion -->
<receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="net.kenyang.android" />
</intent-filter>

<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="net.kenyang.android" />
</intent-filter>
</receiver>



第二部分就是加入應有的permission,
<!-- 下面的net.kenyang記得要改成自己的package name -->
<permission android:name="net.kenyang.android.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="net.kenyang.android.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>



【2.  向C2DM註冊 】
 

這一步驟就是啟動C2DM,並告訴C2DM要註冊,
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(SelectLoginMode.this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", "xxxxx@gmail.com"); // 記得這邊要填入當初你在第三篇註冊的MAIL
startService(registrationIntent);


【3.  接收與傳送資料 】 

這一步驟就是寫一個receiver,
這個receiver會

  1. 接收C2DM傳來的registration id,
  2. 也會接收C2DM傳來的message(這個message就是我們Server傳給C2DM的message)
  3. 並且將registration id傳送到我們第三篇實作的server上面去


我們先在receiver中寫一個函數將registration id傳送至我們第三篇實作的server上面去,
server只要收到,就會傳送資料到C2DM上!
private void fnRegistrationDone(final String strRegistrationId){
try{
Thread thread = new Thread(new Runnable() {

@Override
public void run() {
HttpURLConnection conn = (HttpURLConnection) new URL("http://c2dm.kenyang.net/registration").openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET"); // 因為在GAE上面的servlet,我是用get method
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", strRegistrationId.getBytes().length); // 告訴c2dm要傳送的資料長度

// 送出資料
OutputStream out = conn.getOutputStream();
out.write(strRegistrationId.getBytes());
out.close();

conn.fnClose();
// 到這,一個註冊就完成,接著就是等著收資料

}
});
thread.start();

}catch (Exception e) {
}

}



接著開始寫接收資料(registration_id, message)的code,如下:
@Override
public void onReceive(Context context, Intent intent) {

// 接收到C2DM的訊息
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {

String strRegistrationId = intent.getStringExtra("registration_id");
// 如果註冊失敗
if (intent.getStringExtra("error") != null) {

Log.d("Ken.yang", "c2dm registration fail");

// Registration 成功
} else if (strRegistrationId != null) {
// 把registration_id傳到我們server(呼叫我們上面寫的函數)
fnRegistrationDone(strRegistrationId);

}

} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {

// 接收來自C2DM的MESSAGE,這message就是我們server傳給C2DM的 (可以看第三篇)
if (intent.getExtras().containsKey("keyHello")){
strValue = intent.getExtras().getString("keyHello");
Log.d("Ken.yang", "C2DM=== receive" + strValue);
}
}
}



完成上面步驟,就完成c2dm了!
也可以收到訊息了!!!!
因為我們第三篇server上傳送的訊息是fromKen,
所以這裡收到的也是fromKen!!






















blog comments powered by Disqus