GCM registering twice with 2 different regIds

2019-09-07 04:21发布

in my app I have this code to register to the GCM service:

Server.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);
}

when I run this code, I see in the server that my device is registering twice with 2 different regIds, and when the server sends a push, the device receives 2 messages.

is this a normal thing? is there a way to ensure only one regId?

2条回答
爷、活的狠高调
2楼-- · 2019-09-07 04:25

What do you need to check when you want to register is

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");
}
查看更多
相关推荐>>
3楼-- · 2019-09-07 04:25

you Should Go with Below Code to Ensure you are register the RegId to Server only if it is not registerd to Server.

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);
            }
        }

    }

Hope it Will Help.

查看更多
登录 后发表回答