GCM Sender_ID未在澳大利亚注册(收到错误 - Invalid_Sender)

时间:2014-04-09 10:25:07

标签: android google-cloud-messaging sender-id

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

我使用的是在欧洲和美国有效的发送方ID,但在澳大利亚不起作用,怎么可能?

我尝试在澳大利亚注册一个我打开的新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();
        }
    }
}

0 个答案:

没有答案