谁有c2dm的相关资料,最好有实例
谁有c2dm的相关资料,最好有实例
google的开发文档也看过了,但没看明白,感觉很乱
下面是我写的程序,但没获得registration id
- Java code
package com.c2dm_client1;import com.c2dm_client1.R;import android.app.Activity;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.EditText;public class c2dm_client1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent registrationIntent = new Intent("com.google.android.c2dm.REGISTER"); //这里的new intent需要改为自己的intent吗?? registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); registrationIntent.putExtra("sender", "XXXXX@gmail.com"); startService(registrationIntent); EditText et1 = (EditText)findViewById(R.id.editText1); et1.setText("wait for the register id..."); } public void onReceive(Context context, Intent intent){ if ( intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION") ){ handleRegistration(context, intent); }else if ( intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")){ handleReceive(context, intent); } } private void handleRegistration(Context context, Intent intent){ String registration = intent.getStringExtra("registration_id"); String error = intent.getStringExtra("error"); String unregistration = intent.getStringExtra("ungistrered"); if ( error != null ){ //Registration failed,should try again later. EditText et1 = (EditText)findViewById(R.id.editText1); et1.setText(et1.getText() + "error! "); }else if ( unregistration != null ){ //Unregistration done, new messages from the authorized sender will be rejected. EditText et1 = (EditText)findViewById(R.id.editText1); et1.setText(et1.getText() + "unregistration! "); } else if ( registration != null ){ //Send the registration ID to the 3rd party site that is sending the messages. //This should be done in separate thread. //When done,remember that all registration is done. EditText et1 = (EditText)findViewById(R.id.editText1); et1.setText(et1.getText() + registration); } } private void handleReceive(Context context, Intent intent){ EditText et1 = (EditText)findViewById(R.id.editText1); et1.setText(et1.getText() + "Get from the 3rd server! "); }}
- XML code
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.c2dm_client1" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".c2dm_client1" 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> <!-- only this application can receive the c2dm messages --> <permission android:name="com.c2dm_client1.c2dm_client1.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.c2dm_client1.c2dm_client1.permission.C2D_MESSAGE" /> <!-- this application has permission to register and receive message --> <uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE" /> <!-- This application has permission to use the internet.(To send connect the server) --> <uses-permission android:name="android.permission.INTERNET" /> <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> <receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <!-- Receive the actual messages --> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.c2dm_client1.c2dm_client1" /> </intent-filter> <!-- Receive the registration id --> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.c2dm_client1.c2dm_client1" /> </intent-filter> </receiver></manifest>
[解决办法]
http://blog.csdn.net/ichliebephone/article/details/6591071