2010-09-21 140 views
1

我正在使用版本8的模擬器,當我嘗試註冊Android應用程序時,它會給出空註冊ID。請幫我解決這個問題。我如何使用C2DM註冊Android應用程序。我的代碼是註冊App。使用C2DM註冊Android應用程序

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
registrationIntent.putExtra("app", PendingIntent.getBroadcast(startCode.this, 0, new Intent(), 0)); // boilerplate 
registrationIntent.putExtra("sender", "[email protected]"); 
startService(registrationIntent); 
handleRegistration(getApplicationContext(), registrationIntent); 


private void handleRegistration(Context context, Intent intent) { 
     String registration = intent.getStringExtra("registration_id"); 
     if (intent.getStringExtra("error") != null) { 
      // Registration failed, should try again later. 
      Log.e("ERROR", "ERROR"); 
     } else if (intent.getStringExtra("unregistered") != null) { 
      // unregistration done, new messages from the authorized sender will be rejected 
      Log.e("unregistered", "unregistered"); 
     } else if (registration != null) { 
      // Send the registration ID to the 3rd party site that is sending the messages. 
      // This should be done in a separate thread. 
      // When done, remember that all registration is done. 
      Log.e("registration", registration); 
     } 
    } 

回答

1

根據我的經驗,您需要在模擬器中添加一個Google帳戶。

您只能以Google API 8而不是Android 2.2爲目標。

+0

我正在使用Google API 8,並且我已經在模擬器中創建帳戶,但是如何將通知發送到我的模擬器 – 2010-09-27 07:07:20

+0

註冊過程完全自動化並且在模擬器中正常工作。如上所述,您需要設置一個Google帳戶,但是您的註冊碼應該會導致註冊響應(從而導致C2DM ID)。如果你沒有收回ID,那麼你錯過了其他的東西(一個許可,也許?check logcat)。 – mbafford 2011-03-07 14:32:22

+0

通過C2DM通道向模擬器發送「通知」仍然需要Google服務器才能正常工作。沒有「模擬」C2DM消息選項,您必須使用此處列出的說明通過Google服務器發送一個:http://code.google.com/android/c2dm/index.html#server – mbafford 2011-03-07 14:34:56