GCM注册失败

时间:2016-01-21 10:04:54

标签: android google-cloud-messaging

我正在使用谷歌推送通知。我能够检索我的注册码,但是它未能在服务器中注册我的手机。有人可以帮助我在哪里出错

主要活动:

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

1 个答案:

答案 0 :(得分:0)

请使用最新的gcm jar文件。如果您收到错误“内部错误:重试接收器类尚未设置”,那么您有一个旧版本的客户端gcm.jar。您可以从GCM存储库下载源代码或更新的gcm.jar。

您可以使用本教程作为指南,该项目包含用于探索GCM API的客户端库和示例:https://github.com/google/gcm