2013-12-09 24 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)功能。