Pergunta

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 ?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top