2016-02-12 106 views
-5

我想在我的项目中使用GCM我读到关于GCM的内容我了解它的工作原理,但是当我无法理解我们如何执行代码以获取注册身份证表格GCM你可以给我完整的代码从GCM获取注册ID,请不要说这样做谷歌。 请给我代码如何在GCM上注册,我如何从GCM获得注册ID。如何注册gcm从gcm获得注册ID

+0

看一看样品https://github.com/googlesamples/google-services/tree/master/android/gcm –

+3

*请给我的代码,我怎么能在GCM注册,我怎样才能得到来自GCM *的注册ID,真的吗? – Rohit5k2

+0

@ Rohit5k2 meh,只是为了这一行,我给了他*完整的代码*期待他不让它工作... – Shark

回答

1

在这里,你去。

private void registerInBackground() { 

    gcmRegistrationInProcces = true; 

    new AsyncTask<Void, Void, String>() { 

     @Override 
     protected String doInBackground(Void... params) { 
      String msg = ""; 
      try { 
       if (Application.gcm == null) { 
        Application.gcm = GoogleCloudMessaging.getInstance(getApplicationContext()); 
       } 
       GCM_REGISTRATION_ID = Application.gcm.register(GCM_SENDER_ID); 
       msg = "Device registered, registration ID=" + GCM_REGISTRATION_ID; 

       // 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(getApplicationContext(), GCM_REGISTRATION_ID); 
      } 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; 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      gcmRegistrationInProcces = false; 

      super.onPostExecute(s); 
     } 

     @Override 
     protected void onPostExecute(String msg) 
     { 
      Toast.makeText(getBaseContext(), "Registered with REG_ID:\n"+GCM_REGISTRATION_ID, Toast.LENGTH_SHORT).show(); 
     } 
    }.execute(null, null, null); 
} 
+0

问我有一个注册页面,有三个领域的注册我去了另一个活动,现在我想使用GCM和我读了关于发件人ID,项目ID和API密钥如何和在哪里我可以使用这些东西 –

+0

并告诉我这三件事情是必要的? –

+0

感谢您的回复 –