2013-03-17 60 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行即可解决您的问题。

相关问题