2016-01-21 213 views
1

我正在使用Google推送通知。我能夠找回我的REG ID,但其未能註冊我的server.Can有人移動幫助我我要去哪裏錯了GCM註冊失敗

主要活動:

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); 
    //setContentView(R.layout.activity_main); 
    // mDisplay = (TextView) findViewById(R.id.display); 
    registerReceiver(mHandleMessageReceiver, 
      new IntentFilter(DISPLAY_MESSAGE_ACTION)); 
    final String regId = GCMRegistrar.getRegistrationId(this); 
    mDisplay.append("Reg id" + regId + " "); 
    if (regId.equals("")) { 
     // Automatically registers application on startup. 
     GCMRegistrar.register(this, SENDER_ID); 
     //mDisplay.append("Reg id" + regId + " "); 
    } else { 
     // Device is already registered on GCM, check server. 
     if (GCMRegistrar.isRegisteredOnServer(this)) { 
      // Skips registration. 
      //mDisplay.append(getString(R.string.already_registered) + 
      "\n"); 
      // mDisplay.append("Reg id" + regId + " "); 
     } 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. 
      // mDisplay.append("Reg id" + regId + " "); 
      final Context context = this; 
      mRegisterTask = new AsyncTask<Void, Void, Void>() { 

       @Override 
       protected Void doInBackground(Void... params) { 
        boolean registered = 
          ServerUtilities.register(context, regId, 
        userNum); 
        // 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); 
     } 
    } 

服務器實用程序:

for (int i = 1; i <= MAX_ATTEMPTS; i++) { 
     Log.d(TAG, "Attempt #" + i + " to register"); 
     try { 
      displayMessage(context, context.getString(
        R.string.server_registering, i, MAX_ATTEMPTS)); 
      post(serverUrl, params); 
      GCMRegistrar.setRegisteredOnServer(context, true); 
      String message = context.getString(R.string.server_registered); 
      CommonUtilities.displayMessage(context, message); 
      return true; 
     } catch (IOException e) { 
      // Here we are simplifying and retrying on any error; in a real 
      // application, it should retry only on unrecoverable errors 
      // (like HTTP error code 503). 
      Log.e(TAG, "Failed to register on attempt " + i, e); 
      if (i == MAX_ATTEMPTS) { 
       break; 
      } 
      try { 
       Log.d(TAG, "Sleeping for " + backoff + " ms before retry"); 
       Thread.sleep(backoff); 
      } catch (InterruptedException e1) { 
       // Activity finished before we complete - exit. 
       Log.d(TAG, "Thread interrupted: abort remaining retries!"); 
       Thread.currentThread().interrupt(); 
       return false; 
      } 
      // increase backoff exponentially 
      backoff *= 2; 
     } 
    } 

服務器和發件人ID:

static final String SERVER_URL = "http://http://xxx.xxx.xxx.xxx:8080/Sample"; 


/** 
* Google API project id registered to use GCM. 
*/ 
static final String SENDER_ID = "921198929963"; 

錯誤日誌:

01-21 15:32:59.359 19009-19090/? E/GCMRegistrar: internal error: retry 
    receiver class not set yet 
01-21 15:32:59.365 19009-19090/? E/Regid: 
APA91bEpJzZoMsj10CzBHXIAxmkyJMyZXTyCUsihO_BhmB-noqc-Llu5uSiaXZBF- 
ICCjHtwgNTysoOXyA9715U1kG4L2BV-Zl3vz5DOwk4mTYX9HdF5aBSaOzmLo9zE_ZDz-9ppnThi 
01-21 15:32:59.988 19009-19090/? E/SurakshaGCM: Failed to register on 
attempt 1 
01-21 15:33:02.215 19009-19090/? E/SurakshaGCM: Failed to register on 
attempt 2 
01-21 15:33:06.647 19009-19090/? E/SurakshaGCM: Failed to register on 
attempt 3 
01-21 15:33:14.913 19009-19090/? E/SurakshaGCM: Failed to register on 
attempt 4 

回答

0

請使用最新的gcm jar文件。如果您收到錯誤「內部錯誤:重試接收器類尚未設置」,那麼您的客戶端版本爲gcm.jar。您可以從GCM存儲庫下載源代碼或更新的gcm.jar。

您可以使用此教程作爲指南,該項目包含客戶端庫和示例來探索GCM的API:https://github.com/google/gcm