2013-12-09 30 views
0

我想在我的android應用程序上實現谷歌的GCM,我遇到了關於.register()函數的問題。Android GCM:.register功能需要服務器嗎?

private void registerInBackground() { 
    new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 
      String msg = ""; 
      try { 
       if (gcm == null) { 
        gcm = GoogleCloudMessaging.getInstance(context); 
       } 
       regid = gcm.register(SENDER_ID); 
       Log.d(TAG, "registered gcm"); 
       msg = "Device registered, registration ID=" + regid; 
       Log.d(TAG, "Current Device's Registration ID is: "+msg); 

       // You should send the registration ID to your server over HTTP, so it 
       // can use GCM/HTTP or CCS to send messages to your app. 
       // sendRegistrationIdToBackend(); 

       // For this demo: we don't need to send it because the device will send 
       // upstream messages to a server that echo back the message using the 
       // 'from' address in the message. 

       // Persist the regID - no need to register again. 
       storeRegistrationId(context, regid); 
      } catch (IOException ex) { 
       msg = "Error :" + ex.getMessage(); 
       // If there is an error, don't just keep trying to register. 
       // Require the user to click a button again, or perform 
       // exponential back-off. 
      } 
      return msg; 
     } 

對於REGID = gcm.register(SENDER_ID);,將應用程序利用發送何種類型的連接到GCM以註冊SENDER_ID?它只能通過Wi-Fi來完成,還是需要建立一個服務器才能註冊GCM。我的主要問題是,客戶端應用程序如何向GCM註冊以獲取註冊ID?

回答

1

AFAIR,不,它不需要從你身邊的定製服務器來接收Registration ID。 Google服務器將其發送給用戶設備。您(您的自定義服務器)稍後將需要此ID來請求Google向用戶設備發送推送通知。用戶設備將使用此特定的registration ID進行標識。所以你的服務器需要存儲它。您可以在服務器端將registration ID映射爲某種友好名稱。

簡而言之,您需要一個自定義服務器,當它從Google接收到用戶設備時,您將向其發送註冊ID。但是,您不需要它(.register)功能。