0

具有特定发件人 ID 的 GCM 注册能否在一个国家/地区获得响应“Invalid_Sender”,但在另一个国家/地区会起作用?

我使用的 senderID 在欧洲和美国有效,但在澳大利亚无效,这怎么可能?

我尝试使用我打开的新 SenderID 在澳大利亚注册并且它可以工作,但是在尝试使用旧的 SenderID 时它不起作用,尽管它在美国和我在欧洲尝试过的一些地方工作。

如果有人对此有所了解,将不胜感激。

谢谢

这是我用来注册的方法:

public void startGCMRegistration() {
    if (checkPlayServices()) {
         mGCM = GoogleCloudMessaging.getInstance(this);
         String regId = getRegistrationId();

    if (regId.isEmpty()) {
           registerInBackground();
    } else {
           Logger.write("GCM startGCMRegistration(): Already registered. RegID: " + regId, null);
    }
    }
 }


public boolean checkPlayServices() {
    boolean retVal = true;
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        retVal = false;
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {

        } else {

        }
        Logger.write("GCM checkPlayServices(): This device does not support new GCM implementation.", null);
    }
    return retVal;
}


public String getRegistrationId() {
    final SharedPreferences prefs = getGCMPreferences(mContext);
    String registrationId = prefs.getString(PROPERTY_REG_ID, App.NULL);
    if (registrationId.isEmpty()) {
        Logger.write("GCM getRegistrationId() Registration not found.", null);
        return App.NULL;
    }

    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(mContext);
    if (registeredVersion != currentVersion) {
        Logger.write("GCM getRegistrationId() App version changed.", null);
        return App.NULL;
    }
    return registrationId;
}

private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {           
        throw new RuntimeException("GCM Could not get package name: " + e);
    }
}


  private void registerInBackground() {

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                if (mGCM == null) {
                    mGCM = GoogleCloudMessaging.getInstance(mContext);
                }
                Logger.write("GCM registerInBackground(): SenderID: " + getSenderID(), null);
                mGCM.unregister();
                String regId = mGCM.register(getSenderID());
                Logger.write("GCM registerInBackground: Successfully registered... RegistrationID:" + regId, null);

                storeRegistrationId(mContext, regId);

            } catch (IOException ex) {
                handleGcmError(ex);
            }
            return null;
        }
    }.execute();
}


     public void storeRegistrationId(Context context, String regId) {
    final SharedPreferences prefs = getGCMPreferences(context);
    int appVersion = getAppVersion(context);
    Logger.write("GCM storeRegistrationId() Saving regId on app version " + appVersion, null);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(PROPERTY_REG_ID, regId);
    editor.putInt(PROPERTY_APP_VERSION, appVersion);
    editor.commit();

}


     private void handleGcmError(IOException ex) {
    String error = ex == null ? "" : ex.getMessage();

    Logger.write("GCM handleGcmError() error: " + error, null);

    if (App.getApp().isNetworkingAllowed()) {
        final int gcmRetryCounter = getGCMRetryCount();
        if (gcmRetryCounter < GCM_RETRY_MAX_COUNT) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    sleepThread(60 * 1000);
                    Logger.write("GCM handleGcmError() retry counter = " + gcmRetryCounter, null);

                    startGCMRegistration();
                }
            }).start();
        }
    }
}
4

0 回答 0