0

在我的应用程序中,我有此代码可以注册到 GCM 服务:

服务器.java

protected void registerToGcm() {
    GCMRegistrar.checkDevice(mContext);
    GCMRegistrar.checkManifest(mContext);
    String regId = GCMRegistrar.getRegistrationId(mContext);
    if ("".equals(regId)) {
        GCMRegistrar.register(mContext, GCMIntentService.SENDER_ID);
    } else {
        registerToServer(regId);
    }
}

GCMIntentService.Java

public static final String SENDER_ID = "1111111111";
@Override
protected void onRegistered(Context context, String regId) {
    Log.d(TAG, "Registered to GCM with regId: " + regId);
    Server.registerToServer(regId);
}

当我运行此代码时,我在服务器中看到我的设备使用 2 个不同的设备注册了两次regIds,并且当服务器发送推送时,设备收到 2 条消息。

这是正常现象吗?有没有办法确保只有一个regId

4

2 回答 2

0

注册时需要检查什么是

GCMRegistrar.checkDevice(getApplicationContext());
GCMRegistrar.checkManifest(getApplicationContext());

deviceToken = GCMRegistrar.getRegistrationId(getApplicationContext());

if (printLog == true)
    Log.i("Home", "Registration id =====  " + deviceToken);
if (deviceToken.equals("")) {
    GCMRegistrar.register(getApplicationContext(), SENDER_ID);
} else {
    if (printLog == true)
        Log.v("Home", "Device Already registered");
}
于 2013-02-18T12:22:23.537 回答
0

您应该使用以下代码以确保仅在未注册到服务器时才将 RegId 注册到服务器。

public void GCMWebService(final String regId) {
        if (regId.equals("")) {
            Log.i(TAG,
                    "================Inside if in regId=null  GCMWebService==============================");
            // Automatically registers application on startup.
             GCMRegistrar.register(mContext, GCMIntentService.SENDER_ID);
            // GCMRegistrar.unregister(this);
        } else {
            Log.i(TAG,
                    "================Inside else in regId=null  GCMWebService==============================");
            // Device is already registered on GCM, needs to check if it is
            // registered on our server as well.
            if (GCMRegistrar.isRegisteredOnServer(this)) {
                // Skips registration.
                Log.i(TAG,
                        "================Inside else in regId=null Already register on Server GCMWebService=============================");
                // mDisplay.append(getString(R.string.already_registered) +
                // "\n");
            } else {
                Log.i(TAG,
                        "================Inside else in regId=null trying to  register on Server GCMWebService=============================");
                // 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) {
                        Log.i(TAG,
                                "================Inside doInBackground Method       GCMWebService==============================");
                           registerToServer(regId);
                        Log.i(TAG,
                                "================Server side REegistered or not?"
                                        + registered);
                        // 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) {
                            Log.i(TAG,
                                    "================Inside unregister inside Do in backgroound GCMWebService==============================");
                            GCMRegistrar.unregister(context);
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        Log.i(TAG,
                                "================Inside onPostExecute Method   GCMWebService==============================");
                        mRegisterTask = null;
                        if (!registered) {
                            /*
                             * final Toast tag = Toast .makeText( context,
                             * "Please Check Your Internet Connection and Try again \nOr\nWebservice is not Working now..."
                             * , Toast.LENGTH_SHORT);
                             */

                            new CountDownTimer(8000, 1000) {
                                public void onTick(long millisUntilFinished) {
                                    Toast.makeText(
                                            context,
                                            "Please Check Your Internet Connection and Try again \nOr\nWebservice is not Working now...",
                                            Toast.LENGTH_SHORT).show();
                                }

                                public void onFinish() {
                                }

                            }.start();
                        }
                    }

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

    }

希望它会有所帮助。

于 2013-02-18T11:52:17.847 回答