2014-03-04 92 views

回答

0

,請使用以下功能:

public static String registerClient(Activity activity) { 
     String regId = ""; 
     String registrationStatus; 
     try { 
      // Check that the device supports GCM (should be in a try/catch) 
      GCMRegistrar.checkDevice(activity); 

      // Check the manifest to be sure this app has all the required 
      // permissions. 
      GCMRegistrar.checkManifest(activity); 

      // Get the existing registration id, if it exists. 
      regId = GCMRegistrar.getRegistrationId(activity); 

      if (regId.equals("")) { 

       registrationStatus = "Registering..."; 

       // register this device for this project 
       GCMRegistrar.register(QwichesApplication.mContext, 
         CommonVariable.GCM_PROJECTID); 
       regId = GCMRegistrar.getRegistrationId(activity); 

       registrationStatus = "Registration Acquired"; 
       Log.d("tag", regId); 
       // This is actually a dummy function. At this point, one 
       // would send the registration id, and other identifying 
       // information to your server, which should save the id 
       // for use when broadcasting messages. 

      } else { 

       registrationStatus = "Already registered"; 

      } 

     } catch (Exception e) { 

      e.printStackTrace(); 
      registrationStatus = e.getMessage(); 

     } 

     Log.d("tag", regId); 

     // This is part of our CHEAT. For this demo, you'll need to 
     // capture this registration id so it can be used in our demo web 
     // service. 

     return regId; 

    } 

該函數返回gcm_id。 我希望它對你有用...

+0

謝謝dipali ..其實我想連接到web應用程序和存儲gcm id在mysql數據庫變得越來越困難,因爲我是一個非常新的android和gcm以及 – user3377410

+0

我認爲我的代碼對你有幫助。如果它確實有幫助,那麼請接受這個答案。 – dipali