0

我按照本教程http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html 我编写了清单文件,提供了所有足够的权限。但是我的注册 ID 为空。请帮助这是我写的代码:

public class C2DMClientActivity extends Activity {
/** Called when the activity is first created. */
final static String AUTH="authentication";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
public void register(View view){//Register Button onclick event
    Log.w("C2DM","startRegistrationProcess");
    Intent intent=new Intent("com.google.android.c2dm.intent.REGISTER");
    intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
    intent.putExtra("sender","shubhamjindal18@gmail.com");
    startService(intent);
}
public void showRegistrationId(View view){//Show button onclick event
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
    String string=prefs.getString(AUTH,"n/a");
    Toast.makeText(this,string,Toast.LENGTH_LONG).show();
    Log.d("C2DM RegId",string);
}
}
public class C2DMRegistrationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
    String string=intent.getAction();
    Log.w("C2DM","Registration Receiver Called");
    if ("com.google.android.c2dm.intent.REGISTRATION".equals(string)){
        Log.w("C2DM", "Received registration ID");
        final String str=intent.getStringExtra("registation_id");
        final String error=intent.getStringExtra("error");
        Log.d("C2DM", "dmControl: registrationId = " + str
                + ", error = " + error);
        String deviceId = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);
        createNotification(context, str);
        sendRegistrationIdToServer(deviceId,str);

        saveRegistrationId(context,str);
    }
}
public void saveRegistrationId(Context context,String str){
    SharedPreferences   prefs=PreferenceManager.getDefaultSharedPreferences(context);
    Editor edit=prefs.edit();
    edit.putString(C2DMClientActivity.AUTH,str);
    edit.commit();
}
public void createNotification(Context context,String str){
    NotificationManager notim=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification noti=new Notification(R.drawable.ic_launcher,"Registation Successful",System.currentTimeMillis());
    noti.flags|=Notification.FLAG_AUTO_CANCEL;
    Intent intent=new Intent(context,RegistrationResultActivity.class);
    intent.putExtra("registration_id", str);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);
    noti.setLatestEventInfo(context, "Registration",
            "Successfully registered", pendingIntent);
    notim.notify(0, noti);
}
public void sendRegistrationIdToServer(String deviceId,
        String registrationId) {
    Log.d("C2DM", "Sending registration ID to my application server");
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        // Get the deviceID
        nameValuePairs.add(new BasicNameValuePair("deviceid", deviceId));
        nameValuePairs.add(new BasicNameValuePair("registrationid",
                registrationId));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
            Log.e("HttpResponse", line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

public class RegistrationResultActivity extends Activity {
 @Override
    public void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity);
        Bundle extras=getIntent().getExtras();
        if (extras!=null){
            String str=extras.getString("registration_id");
            TextView tv=(TextView) findViewById(R.id.textView1);
            tv.setText(str);
        }
        super.onCreate(savedInstanceState);
    }
}
public class C2DMMessageReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
    String action=intent.getAction();
    Log.w("C2DM","Message Receiver called");
    if ("com.google.android.c2dm.intent.RECEIVE".equals(action)){
        Log.w("C2DM", "Received message");
        final String payload = intent.getStringExtra("payload");
        Log.d("C2DM", "dmControl: payload = " + payload);
        createNoti(context,payload);
    }
}
public void createNoti(Context context,String str){
    NotificationManager notim=(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    Notification noti=new Notification(R.drawable.ic_launcher,"Message received",System.currentTimeMillis());
    noti.flags|=Notification.FLAG_AUTO_CANCEL;
    Intent intent=new Intent(context,MessageResultActivity.class);
    intent.putExtra("payload",str);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);
    noti.setLatestEventInfo(context, "Message",
            "New message received", pendingIntent);
    notim.notify(0, noti);
}
}
public class MessageResultActivity extends Activity{

 @Override
    public void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity);
        Bundle extras=getIntent().getExtras();
        if (extras!=null){
            String payload=extras.getString("payload");
            TextView tv=(TextView) findViewById(R.id.textView1);
            tv.setText(payload);
        }
        super.onCreate(savedInstanceState);
    }
}
4

1 回答 1

1

应该是registration_id,但它是registation_id

 final String str=intent.getStringExtra("registation_id");

应该

final String str=intent.getStringExtra("registration_id ");

https://developers.google.com/android/c2dm/#push

于 2012-06-20T05:35:40.793 回答