issue in getting token id in android

孤人 提交于 2019-12-25 02:18:05

问题


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:


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.



来源:https://stackoverflow.com/questions/21931926/issue-in-getting-token-id-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!