0

我知道对此有很多问题,但我无法从这些问题中找出解决方案。

我在来自 GCM 的令牌中得到空值。很多人都使用类做到了这一点,但我在同一个类的后台线程中这样做。它在 regId 中返回 null。

创建时

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()。

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

我试图记录它在 regId 中显示 null 的错误。这可能是什么问题?

4

1 回答 1

0

有些我的应用程序 ID 无法正常工作。在谷歌控制台上重新创建应用程序生成了一个我在项目中使用的新 ID,它开始工作。

于 2014-03-01T09:16:47.700 回答