Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. public void startGCMRegistration() {
  2. if (checkPlayServices()) {
  3. mGCM = GoogleCloudMessaging.getInstance(this);
  4. String regId = getRegistrationId();
  5.  
  6. if (regId.isEmpty()) {
  7. registerInBackground();
  8. } else {
  9. Logger.write("GCM startGCMRegistration(): Already registered. RegID: " + regId, null);
  10. }
  11. }
  12. }
  13.  
  14.  
  15. public boolean checkPlayServices() {
  16. boolean retVal = true;
  17. int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  18. if (resultCode != ConnectionResult.SUCCESS) {
  19. retVal = false;
  20. if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  21.  
  22. } else {
  23.  
  24. }
  25. Logger.write("GCM checkPlayServices(): This device does not support new GCM implementation.", null);
  26. }
  27. return retVal;
  28. }
  29.  
  30.  
  31. public String getRegistrationId() {
  32. final SharedPreferences prefs = getGCMPreferences(mContext);
  33. String registrationId = prefs.getString(PROPERTY_REG_ID, App.NULL);
  34. if (registrationId.isEmpty()) {
  35. Logger.write("GCM getRegistrationId() Registration not found.", null);
  36. return App.NULL;
  37. }
  38.  
  39. int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
  40. int currentVersion = getAppVersion(mContext);
  41. if (registeredVersion != currentVersion) {
  42. Logger.write("GCM getRegistrationId() App version changed.", null);
  43. return App.NULL;
  44. }
  45. return registrationId;
  46. }
  47.  
  48. private static int getAppVersion(Context context) {
  49. try {
  50. PackageInfo packageInfo = context.getPackageManager()
  51. .getPackageInfo(context.getPackageName(), 0);
  52. return packageInfo.versionCode;
  53. } catch (NameNotFoundException e) {
  54. throw new RuntimeException("GCM Could not get package name: " + e);
  55. }
  56. }
  57.  
  58.  
  59. private void registerInBackground() {
  60.  
  61. new AsyncTask<Void, Void, Void>() {
  62. @Override
  63. protected Void doInBackground(Void... params) {
  64. try {
  65. if (mGCM == null) {
  66. mGCM = GoogleCloudMessaging.getInstance(mContext);
  67. }
  68. Logger.write("GCM registerInBackground(): SenderID: " + getSenderID(), null);
  69. mGCM.unregister();
  70. String regId = mGCM.register(getSenderID());
  71. Logger.write("GCM registerInBackground: Successfully registered... RegistrationID:" + regId, null);
  72.  
  73. storeRegistrationId(mContext, regId);
  74.  
  75. } catch (IOException ex) {
  76. handleGcmError(ex);
  77. }
  78. return null;
  79. }
  80. }.execute();
  81. }
  82.  
  83.  
  84. public void storeRegistrationId(Context context, String regId) {
  85. final SharedPreferences prefs = getGCMPreferences(context);
  86. int appVersion = getAppVersion(context);
  87. Logger.write("GCM storeRegistrationId() Saving regId on app version " + appVersion, null);
  88. SharedPreferences.Editor editor = prefs.edit();
  89. editor.putString(PROPERTY_REG_ID, regId);
  90. editor.putInt(PROPERTY_APP_VERSION, appVersion);
  91. editor.commit();
  92.  
  93. }
  94.  
  95.  
  96. private void handleGcmError(IOException ex) {
  97. String error = ex == null ? "" : ex.getMessage();
  98.  
  99. Logger.write("GCM handleGcmError() error: " + error, null);
  100.  
  101. if (App.getApp().isNetworkingAllowed()) {
  102. final int gcmRetryCounter = getGCMRetryCount();
  103. if (gcmRetryCounter < GCM_RETRY_MAX_COUNT) {
  104. new Thread(new Runnable() {
  105. @Override
  106. public void run() {
  107. sleepThread(60 * 1000);
  108. Logger.write("GCM handleGcmError() retry counter = " + gcmRetryCounter, null);
  109.  
  110. startGCMRegistration();
  111. }
  112. }).start();
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement