2013-03-17 32 views
1

我的GCM應用程序爲同一設備生成多個註冊。我已經找到了這個,但coud'nt找到任何解決方案。Gcm android應用程序觸發同一設備的多個註冊

這是我的主要活動::

public void onCreate(Bundle savedInstanceState) { 
    GCMRegistrar.checkDevice(this); 
    GCMRegistrar.checkManifest(this); 
    final String regId = GCMRegistrar.getRegistrationId(this); 
    if (regId.equals("")) { 
     GCMRegistrar.register(this, "952039800261"); 
    } else { 
     Log.v(TAG, "Already registered"); 

    }} 

這是我onRegistered()方法::

protected void onRegistered(Context arg0, final String regId) { 

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
// sets the app name in the intent 
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 
registrationIntent.putExtra("sender", senderId); 
startService(registrationIntent); 


    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.ludlowcastle.co.in/moksha/register.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("regId", regId)); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

我已經得到了同樣的裝置周圍5K註冊的ID我的服務器上..

回答

0

您正在註冊一個無限循環。

我不使用GCMRegistrar類,但我敢肯定的是GCMRegistrar.register(this, "952039800261");做同樣的,因爲這些線路:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
// sets the app name in the intent 
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 
registrationIntent.putExtra("sender", senderId); 
startService(registrationIntent); 

在我的應用程序,這4條線路都在我的主要的onCreate方法活動,而不是撥打GCMRegistrar

當調用onRegistered時,這意味着註冊已成功並且您有一個註冊ID。由於您要求使用此方法重新註冊,因此會向Google發送新的註冊請求,然後在下次註冊成功時再次調用此方法。我不確定您是否在每次通話中獲得不同的註冊ID,或者如果您獲得相同的註冊ID,但只需從onRegistered中刪除提到的4行即可解決您的問題。

相關問題