issue in getting token id in android

2019-09-15 00:50发布

I know that there are lot of questions about this but I couldn't figure out the solution from those questions.

I am getting null in a token from GCM. many people have done this using class, but I am doing this in background thread in same class. It returns null in regId.

OnCreate

if (checkPlayServices()) {
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(context);

        if (regid.isEmpty()) {
            Log.e(TAG, "registering in background");
            registerInBackground();
        } else {
            Log.e(TAG, "Notification Token : " + regid);
            user.setNotificationToken(regid);
        }
    } else {
        MyLog.i(TAG, "No valid Google Play Services APK found.");
    }

RegisterInBackgroud() if device is not registered before.

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {

                Log.e(TAG, "doing in background");
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }

                if(gcm != null)
                {
                    Log.e(TAG, "GCM is not null");
                }
                regid = gcm.register(SENDER_ID);

                Log.e(TAG, "token id:" + regid);

                msg = "Device registered, registration ID=" + regid;

                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

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

I tried to log it bug it display null in regId. what could be the problem in this ?

1条回答
贼婆χ
2楼-- · 2019-09-15 01:19

Some how my app id was not working. Recreating app on google console generated a new id which i used in my project and it started working.

查看更多
登录 后发表回答