2013-01-11 19 views
6
private void registerClient() { 
    try { 
     GCMRegistrar.checkDevice(this); 
     GCMRegistrar.checkManifest(this); 
     regId = GCMRegistrar.getRegistrationId(this); 
     if (regId.equals("")) { 
      registrationStatus = "Registering..."; 
      GCMRegistrar.register(this, "1074338555805"); 
      regId = GCMRegistrar.getRegistrationId(this); 
      registrationStatus = "Registration Acquired"; 
      sendRegistrationToServer(); 
     } else { 
      registrationStatus = "Already registered"; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     registrationStatus = e.getMessage(); 
    } 
} 

id đăng ký là null từ gcm trong android 2.3.3, 2.3 .5 nhưng nó không phải là null và làm việc trong android 2.3.4, 4.0.4.id đăng ký là null từ gcm trong android 2.3.3, 2.3.5 nhưng không phải là null và hoạt động trong android 2.3.4, 4.0.4

+1

Liệu sự giúp đỡ này? http://stackoverflow.com/questions/11713363/i-can-not-get-registration-id-from-android-gcm – trojanfoe

+0

Tôi có cùng một vấn đề –

+1

Câu trả lời hàng đầu trong nhận xét của @ trojanfoe chứa giải pháp của bạn. 'GCMRegistrar.register' là không đồng bộ và vì vậy gọi' getRegistrationId' ngay lập tức sau khi không đáng tin cậy. Xử lý chương trình phát mà Google Services gửi cho bạn với id. – tophyr

Trả lời

0

Như đã nêu rõ trên hướng dẫn thiết lập Dịch vụ của Google Play.

https://developer.android.com/google/gcm/client.html

Application OnCreate:

// Check device for Play Services APK. If check succeeds, proceed with 
    // GCM registration. 
    if (checkPlayServices()) { 
     gcm = GoogleCloudMessaging.getInstance(this); 
     regid = getRegistrationId(context); 

     if (regid.isEmpty()) { 
      registerInBackground(); 
     } 
    } else { 
     Log.i(TAG, "No valid Google Play Services APK found."); 
    } 

ký trong mã nền:

/** 
* Registers the application with GCM servers asynchronously. 
* <p> 
* Stores the registration ID and app versionCode in the application's 
* shared preferences. 
*/ 
private void registerInBackground() { 
    new AsyncTask() { 
    @Override 
    protected String doInBackground(Void... params) { 
     String msg = ""; 
     try { 
      if (gcm == null) { 
       gcm = GoogleCloudMessaging.getInstance(context); 
      } 
      regid = gcm.register(SENDER_ID); 
      msg = "Device registered, registration ID=" + regid; 

      // 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. 
      // The request to your server should be authenticated if your app 
      // is using accounts. 
      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; 
    } 

    @Override 
    protected void onPostExecute(String msg) { 
     mDisplay.append(msg + "\n"); 
    } 
    }.execute(null, null, null); 
    ... 
} 
Các vấn đề liên quan