2016-02-12 157 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

感謝您的回覆 –