更新:2013/04/01
發現有很多人搜尋到這篇,C2DM已經更名為GCM(Google Cloud Messaging for Android)
幫大家到找一個比較詳細的說明及操作,大家參考吧
參考資料:
=========================================================================================================================================
是Google還蠻新的技術,可以透過雲端傳訊到你的手機,詳細說明可以到官網看
https://developers.google.com/android/c2dm/
這項技術需要一些元件,一些限制
Android: 要在2.2以上的手機,且手機需要一個Google帳戶使用者
C2DM Server: 需要去官方去做認證(中間的溝通)
Server: 開發者的Server,主要可以從Server發送訊息出去
Manifest設定
首先先將Android的Manifest做設定,先設定好權限
<!-- Only this application can receive the messages and registration result --> <permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" /> <!-- This app has permission to register and receive message --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- Send the registration id to the server --> <uses-permission android:name="android.permission.INTERNET" />
在<application>裡面加入下面
<service android:name=".C2DMReceiver" />
<!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 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="com.example.myapp" /> </intent-filter> <!-- Receive the registration id --> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.example.myapp" /> </intent-filter> </receiver>
*紅色這邊要注意一下裡面的紅字粗體需要和你的Manifest檔的package的名稱一樣
*藍色部份是官方的一個的檔案(待會再程式再說明)
*橘色部份是待會接收訊息
官方資料:https://developers.google.com/android/c2dm/#manifest
接下來開始寫程式的部份
在這裡我們使用Google的Lib也就是剛剛提到的藍字部份可以點我下載
檔案放至成這樣
在裡面打上下面的Code
package your.package; import java.io.IOException; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.google.android.c2dm.C2DMBaseReceiver; public class C2DMReceiver extends C2DMBaseReceiver{ private static final String TAG="C2DMReceiver"; public C2DMReceiver() { super(C2DMActivity.SENDER_ID); } public C2DMReceiver(String senderId) { super(senderId); // TODO Auto-generated constructor stub } @Override protected void onMessage(Context context, Intent intent) { // TODO Auto-generated method stub Log.v(TAG, "C2DMReceiver message"); } } @Override public void onError(Context context, String errorId) { // TODO Auto-generated method stub Log.v(TAG, "C2DMReceiver error"); } @Override public void onRegistered(Context context, String registrationId) throws IOException { // TODO Auto-generated method stub super.onRegistered(context, registrationId); Log.v(TAG, "C2DMReceiver Register:"+registrationId); } @Override public void onUnregistered(Context context) { // TODO Auto-generated method stub super.onUnregistered(context); Log.v(TAG, "C2DMReceiver UnRegister"); } }
大概說明一下
onMessage主要是接收到訊息的會進入的function
onRegistered註冊的時候,這裡要注意一下registrationId,因為在這溝通需要這一組registrationId才能將訊息傳送到App所以在這裡用Log標示出來registrationId
在這裡用一個比較的簡單的方式去寫接收到訊息
@Override protected void onMessage(Context context, Intent intent) { // TODO Auto-generated method stub Log.v(TAG, "C2DMReceiver message"); Bundle extras = intent.getExtras(); if(extras!=null){ String msg = (String)extras.get(C2DMActivity.MESSAGE_KEY_ONE); Log.v(TAG, "The received msg = "+msg); NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, C2DMActivity.class), 0); notification.setLatestEventInfo(this, getString(R.string.app_name), msg, contentIntent); notificationManager.notify(0, notification); } }
接到訊息後會跳出一個訊息顯示
基本上在Android 的C2DM的建置已經完成了,接下來回到主要的Activity.java去註冊C2DM
public class C2DMActivity extends Activity { /** Called when the activity is first created. */ //Debugging private static final String TAG = "C2DM"; private static final boolean D = true; public static final String SENDER_ID = "your email"; //使用C2DM服務的用戶帳號 public static final String MESSAGE_KEY_ONE = "message"; //與Server接收消息的key值 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v(TAG, "Start"); C2DMessaging.register(this, SENDER_ID); //register } }
其中的SENDER_ID只是的你的email也就是等一下會介紹的與C2DM官網註冊的Email
這裡建議不用要個人的Email可以去另外申請一個C2DM專用的
MESSAGE_KEY_ONE這邊是所接收的KEY值必需要相同的名子,也是等會Server那邊會介紹到
到這邊Android的部份就完成了
Android Code:C2DMDemo
有興趣可以到以下的參考連結:
http://blog.kenyang.net/2011/12/android-c2dm.html :)
http://blog.kenyang.net/2011/12/android-c2dm_22.html :P
http://blog.kenyang.net/2012/03/android-c2dm-sever.html :D
http://blog.kenyang.net/2012/03/android-c2dm-android.html XD
沒有留言:
張貼留言