GCM始终返回SENDER_INVALID

时间:2013-11-16 20:51:41

标签: java android google-cloud-messaging

GCM始终使用以下代码返回SENDER_INVALID ..

我所能找到的只是项目ID有效,这是有效的,我也尝试过更改项目,但这没有帮助。

我还尝试了整个stackoverflow和google群组,也改变了谷歌帐户,并尝试使用旧的和新的Google API控制台创建应用程序。

public class GCM {
    private static final String TAG = "GCM";
    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    Context mContext=null;
    Activity mActivity=null;
    String SENDER_ID = "44843754432";
    GoogleCloudMessaging gcm;
    String regid;
    public GCM(Context ctx, Activity act){
        mContext=ctx;
        mActivity=act;
        gcm = GoogleCloudMessaging.getInstance(ctx);
    }
    public String getRegistrationId(Context context) {
        final SharedPreferences prefs = context.getSharedPreferences("DealsGeo_GCM", Context.MODE_PRIVATE);
        String registrationId = prefs.getString(PROPERTY_REG_ID, PROPERTY_REG_ID);
        if (registrationId.isEmpty()) {
            Log.i(TAG, "Registration not found.");
            return "";
        }
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }
    public boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, mActivity, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i(TAG, "This device is not supported.");
                return false;
            }
            return false;
        }
        return true;
    }
    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (NameNotFoundException e) {
            throw new RuntimeException("Could not get package name: " + e);
        }
    }
    public void registerInBackground() {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... arg0) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(mContext);
                    }
                    Log.i(TAG,"Registering "+SENDER_ID);
                    regid = gcm.register(SENDER_ID);
                    storeRegistrationId(mContext, regid);
                } catch (IOException ex) {
                    Log.i(TAG,"Error: "+ex.getMessage());
                }
                return msg;
            }
        }.execute(null, null, null);

    }
    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = context.getSharedPreferences("DealsGeo_GCM", Context.MODE_PRIVATE);
        int appVersion = getAppVersion(mContext);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }
}

2 个答案:

答案 0 :(得分:0)

您有一个类似https://code.google.com/apis/console/?noredirect#project的网址: 581054524740 :在您的项目谷歌控制台中访问,因此您必须输入发件人代码:581054524740

答案 1 :(得分:0)

也许您在复制Google API项目ID时忘记了一个数字。 您的项目ID - 44843754432 - 有11位数,而我看到的所有项目ID都有12位数。