Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement