2015-04-22 56 views
0

嗨,在下面的代碼中,我得到了顯示empty.but用戶名和密碼值的regId,完美地工作。但我不明白爲什麼該設備ID顯示爲空。regId在android中爲空gcm

點擊註冊按鈕後,它顯示已註冊GCM。

public class MainActivity extends Activity { 
    // label to display gcm messages 
    TextView lblMessage; 

    // Asyntask 
    AsyncTask<Void, Void, Void> mRegisterTask; 

    // Alert dialog manager 
    AlertDialogManager alert = new AlertDialogManager(); 

    // Connection detector 
    ConnectionDetector cd; 

    public static String username; 
    public static String password; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     cd = new ConnectionDetector(getApplicationContext()); 

     // Check if Internet present 
     if (!cd.isConnectingToInternet()) { 
      // Internet Connection is not present 
      alert.showAlertDialog(MainActivity.this, 
        "Internet Connection Error", 
        "Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 

     // Getting name, email from intent 
     Intent i = getIntent(); 

     username = i.getStringExtra("username"); 
     password = i.getStringExtra("password");   

     // Make sure the device has the proper dependencies. 
     GCMRegistrar.checkDevice(this); 

     // Make sure the manifest was properly set - comment out this line 
     // while developing the app, then uncomment it when it's ready. 
     GCMRegistrar.checkManifest(this); 

     lblMessage = (TextView) findViewById(R.id.lblMessage); 

     registerReceiver(mHandleMessageReceiver, new IntentFilter(
       DISPLAY_MESSAGE_ACTION)); 

     // Get GCM registration id 
     final String regId = GCMRegistrar.getRegistrationId(this); 

     // Check if regid already presents 
     if (regId.equals("")) { 
      // Registration is not present, register now with GCM   
      GCMRegistrar.register(this, SENDER_ID); 
     } else { 
      // Device is already registered on GCM 
      if (GCMRegistrar.isRegisteredOnServer(this)) { 
       // Skips registration.    
       Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show(); 
      } else { 
       // Try to register again, but not in the UI thread. 
       // It's also necessary to cancel the thread onDestroy(), 
       // hence the use of AsyncTask instead of a raw thread. 
       final Context context = this; 
       mRegisterTask = new AsyncTask<Void, Void, Void>() { 

        @Override 
        protected Void doInBackground(Void... params) { 
         // Register on our server 
         // On server creates a new user 
         ServerUtilities.register(context, username, password, regId); 
         return null; 
        } 

        @Override 
        protected void onPostExecute(Void result) { 
         mRegisterTask = null; 
        } 

       }; 
       mRegisterTask.execute(null, null, null); 
      } 
     } 
    }  

    /** 
    * Receiving push messages 
    * */ 
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String newMessage = intent.getExtras().getString(EXTRA_MESSAGE); 
      // Waking up mobile if it is sleeping 
      WakeLocker.acquire(getApplicationContext()); 

      /** 
      * Take appropriate action on this message 
      * depending upon your app requirement 
      * For now i am just displaying it on the screen 
      * */ 

      // Showing received message 
      lblMessage.append(newMessage + "\n");   
      Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show(); 

      // Releasing wake lock 
      WakeLocker.release(); 
     } 
    }; 

    @Override 
    protected void onDestroy() { 
     if (mRegisterTask != null) { 
      mRegisterTask.cancel(true); 
     } 
     try { 
      unregisterReceiver(mHandleMessageReceiver); 
      GCMRegistrar.onDestroy(this); 
     } catch (Exception e) { 
      Log.e("UnRegister Receiver Error", "> " + e.getMessage()); 
     } 
     super.onDestroy(); 
    } 

} 

logcat的

04-22 04:05:46.389: E/UnRegister Receiver Error(2355): > Receiver not registered: [email protected] 
+0

您是否從Google API Console生成API密鑰? – Piyush

+0

@PiyushGupta是的,我做過 – care567

+0

請注意,C2DM已被棄用,並將於2015年7月30日完全關閉。如果您只是想在您的應用中添加消息,我強烈建議您使用較新的GoogleCloudMessaging API代替。 – Koh

回答

0

創建使用谷歌的控制檯使用相同的名稱作爲Eclipse應用程序的名稱的新項目幫助我解決這個問題。

+0

它不起作用 – care567

+0

問題可能是由於您的註冊ID。你有沒有在控制檯中啓用gcm服務? – Pankaj

+0

是............. – care567

0

試試這個...最前一頁檢查谷歌播放服務

if (checkPlayServices()) { 
     gcm = GoogleCloudMessaging.getInstance(this); 
     mRegistrationId = getRegistrationId(mContext); 

     if (mRegistrationId.isEmpty()) { 
      try { 
       registerInBackground(senderId); 
      } catch (Exception e) { 
       Toast.makeText(mContext, "Unsupported for this   device", Toast.LENGTH_SHORT).show(); 
      } 
     } else { 
      Log.d("TAG", mRegistrationId); 
         } 
    } else { 
     Log.i(TAG, "No valid Google Play Services found."); 
    } 

檢查谷歌播放服務

private boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
     } else { 
      Log.i(TAG, "This device is not supported."); 
      Toast.makeText(mContext, "This device is not supported.", Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 

獲取RegistrationId在這裏

private String getRegistrationId(Context context) { 
    final SharedPreferences prefs = getGCMPreferences(context); 
    String registrationId = prefs.getString(PROPERTY_REG_ID, ""); 
    assert registrationId != null; 
    if (registrationId.isEmpty()) { 
     //Log.i(TAG, "Registration not found."); 
     return ""; 
    } 

    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); 
    int currentVersion = getAppVersion(context); 
    if (registeredVersion != currentVersion) { 
     Log.i(TAG, "App version changed."); 
     return ""; 
    } 
    return registrationId; 
} 

這裏是registerInBackground異步任務

private void registerInBackground(final String senderID) { 

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

     @Override 
     protected String doInBackground(Void... voids) { 
      String msg = ""; 
      try { 
       if (gcm == null) { 
        gcm = GoogleCloudMessaging.getInstance(mContext); 
       } 
       try { 
        mRegistrationId = gcm.register(senderID); 
       } catch (UnsupportedOperationException e) { 
        Toast.makeText(mContext, "Unsupported", Toast.LENGTH_SHORT).show(); 
       } 
       msg = "Device registered, registration ID=" + mRegistrationId; 


      } catch (IOException ex) { 
       msg = "Error :" + ex.getMessage(); 
      } 
      return msg; 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      Log.d(TAG, s); 
     } 

    }.execute(null, null, null); 

} 

這對我有用。希望它會幫助你。

+0

昨天我做了相同的設備它的工作假設設備不支持意味着它將無法工作 – care567

+0

我認爲,只有谷歌播放服務不支持。如果設備有Android。它是受支持的設備。 – Android