如何一次注册到 GCM 并 Post 到我们的服务器?

How to register to GCM and Post to our Server at single attempt?

我已成功注册到 GCM 服务器并将注册 ID 发布到我们的服务器,但需要两次尝试才能完成整个周期。我需要一次性完成所有这些。我在下面添加我的代码,如果我错了请纠正我。 第一次尝试:它将注册到 gcm 服务器。 第二次尝试:它将注册到我们的服务器

protected void RegisterApp() {
    // -----------------------------------
    aServerUtility = (GCMServerController) getApplicationContext();

    // Check if Internet present
    if (!aServerUtility.isConnectingToInternet()) {

        // Internet Connection is not present
        aServerUtility.showAlertDialog(this, "Internet Connection Error",
                "Please connect to Internet connection", false);
        // stop executing code by return
        return;
    }//

    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);

    // Make sure the manifest permissions was properly set
    GCMRegistrar.checkManifest(this);

    // Register custom Broadcast receiver to show messages on activity
    registerReceiver(mHandleMessageReceiver, new IntentFilter(Config.DISPLAY_MESSAGE_ACTION));

    // Get GCM registration id
    final String regId = GCMRegistrar.getRegistrationId(this);
    final String Username = etUserName.getText().toString();

    if (regId.equals("")) {

        // Register with GCM
        GCMRegistrar.register(common_signin.this, Config.GOOGLE_SENDER_ID);

    } else {

        // Device is already registered on GCM Server
        if (GCMRegistrar.isRegisteredOnServer(this)) {

            // Skips registration.

            Toast.makeText(getApplicationContext(),
                    regId + " - Already registered with GCM Server",
                    Toast.LENGTH_LONG).show();
            Log.i(TAG, "Registered device (regId = " + 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.

            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {

                    // Register on our server
                    // On server creates a new user
                    try {
                        aServerUtility.register(context, Username, "", regId);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    return null;
                }

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

            };

            // execute AsyncTask
            mRegisterTask.execute(null, null, null);

        }
    }

    // ---------------------------------------

// Create a broadcast receiver to get message and show on screen
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        String newMessage = intent.getExtras().getString(
                Config.EXTRA_MESSAGE);

        // Waking up mobile if it is sleeping
        aServerUtility.acquireWakeLock(getApplicationContext());

        // Display message on the screen
        // lblMessage.append(newMessage + "");

        Toast.makeText(getApplicationContext(),
                "Got Message: " + newMessage, Toast.LENGTH_LONG).show();

        // Releasing wake lock
        aServerUtility.releaseWakeLock();
    }
};

}

需要调用此方法两次,因为您只在没有现有注册 ID 时调用了 GCMRegistrar.register()(在 if (regId.equals("")) 中)。该方法在此之后结束并且不调用执行服务器注册的 aServerUtility.register()。只有在第二次调用中,当 if (regId.equals("")) 为 false 时,您才真正调用以向您的服务器注册。

说了这么多,请注意 GCMRegistrardeprecated. I would suggest moving to GCM