2013-02-22 63 views
0

在服務器端,我發現一個設備有多個註冊ID,這顯然是爲我創造了很多問題。像收到很多次的消息。GCM:服務器端的多個應用程序ID

我怎麼能得到舊的註冊ID紅色,或確保註冊沒有發生,如果有一個有效的註冊ID。

我跟隨在Android文檔的實例教程的時候,我寫我的應用程序如下:

checkNotNull(SERVER_URL, "SERVER_URL"); 
     checkNotNull(SENDER_ID, "SENDER_ID"); 
     // 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. 
     // NOT required any more GCMRegistrar.checkManifest(this); 

     /** 
     * this code to register reciver moved to message actvity 
     */ 
     //registerReceiver(mHandleMessageReceiver, new IntentFilter(
     //  DISPLAY_MESSAGE_ACTION)); 


     /* final String */regId = GCMRegistrar.getRegistrationId(this); 

     /** 
     * save regId in pref to be used by Location update service 
     */ 
     SavePreferences("regId", regId); 

     if (regId.equals("")) { 
      // Automatically registers application on startup. 
      GCMRegistrar.register(this, SENDER_ID); 
     } else { 
      // Device is already registered on GCM, check server. 
      if (GCMRegistrar.isRegisteredOnServer(this)) { 
       ;; 
       // Skips registration. 
       // -- mDisplay.append(getString(R.string.already_registered) + 
       // "\n"); 
      // System.out.println(getString(R.string.already_registered) 
       //  + "\n"); 

      } 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) { 
         boolean registered = ServerUtilities.register(context, 
           regId); 
         // At this point all attempts to register with the app 
         // server failed, so we need to unregister the device 
         // from GCM - the app will try to register again when 
         // it is restarted. Note that GCM will send an 
         // unregistered callback upon completion, but 
         // GCMIntentService.onUnregistered() will ignore it. 
         if (!registered) { 
          GCMRegistrar.unregister(context); 
         } 
         return null; 
        } 

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

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

編輯:請注意,我可以在設備上接收消息。

回答

1

一段時間,單個設備只有一個註冊ID,多個ID不可能。

當您第一次運行您的應用程序時,您將獲得註冊ID,並且您必須將其註冊到GCM註冊服務商。否則,消息不會傳送到您的設備。

在服務器端,您需要安全地保留此註冊ID,以便您可以在要發送消息時使用它。

您跟蹤特定設備的註冊ID的問題:當您的設備獲得註冊ID時,請將其保存在您的服務器數據庫中,如果您使用其他方式保存該數組並且每當生成新的註冊ID時,請刪除前一個,並將新條目添加到您的數據庫中。

+0

謝謝...這裏的訣竅我如何需要爲相同的設備生成一個新的ID,以便我可以刪除舊的ID? – user836026 2013-02-22 18:30:19

+1

sry .. 當一個新的ID生成時,你會嘗試註冊它的GCM註冊商。 當這個新的註冊ID成功註冊您的onRegister()方法的服務將被調用.checked下面的代碼。 在這裏你可以添加代碼來替換以前的新代碼。 @Override \t保護無效onRegistered(上下文語境,字符串registrationId){ \t \t ServiceUtilities.register(上下文,registrationId); \t} – mahesh 2013-02-22 18:37:07

+1

sry,但我不能正確縮進註釋部分上面的代碼,不能添加此代碼作爲另一個,因爲它反對stackoverflow規則。 從以下時間請問新問題作爲單獨的問題 – mahesh 2013-02-22 18:44:20

相關問題