Android Open Source - Curio_android_SDK Push Util






From Project

Back to project page Curio_android_SDK.

License

The source code is released under:

Apache License

If you think the Android project Curio_android_SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * /*  w w  w  .  jav a2s  .c  om*/
 */
package com.turkcell.curio.utils;

import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.turkcell.curio.CurioClient;

/**
 * 
 * @changed Can Ciloglu
 * @author Ahmet Burak DEMIRKOPARAN
 */
public class PushUtil {

  private static String TAG = "GCMHelper";

  /**
   * Check the device to make sure it has the Google Play Services APK.
   * 
   */
  private static boolean checkPlayServices(Context context) {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    if (resultCode != ConnectionResult.SUCCESS) {
      Log.e(TAG, "This device does not support Google Play Services.");
      return false;
    }
    return true;
  }

  public static void checkForGCMRegistration(final Context context) {
    if (checkPlayServices(context)) {
      String gcmRegistrationId = getStoredRegistrationId(context);
      if (gcmRegistrationId == null) {
        Thread gcmRegisterThread = new Thread(new Runnable() {
          @Override
          public void run() {
            try {
              String registrationId = GoogleCloudMessaging.getInstance(context).register(CurioClient.getInstance().getStaticFeatureSet().getGcmSenderId());
              CurioLogger.d(TAG, "GCM Registration Id acquired: " + registrationId);
              storeRegistrationId(context, registrationId);
              CurioClient.getInstance().sendRegistrationId(context, registrationId);
            } catch (IOException e) {
              CurioLogger.i(TAG, "An error occured while registering/storing GCM registration id.", e);
            }
          }
        });
        gcmRegisterThread.start();
      }
    }
  }

  private static void storeRegistrationId(Context context, String gcmRegistrationId) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARED_PREF_NAME_GCM, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(Constants.SHARED_PREF_KEY_GCM_REGID, gcmRegistrationId);
    editor.putInt(Constants.SHARED_PREF_KEY_APP_VERSION, getAppVersion(context));
    editor.commit();
  }

  public static String getStoredRegistrationId(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARED_PREF_NAME_GCM, Context.MODE_PRIVATE);
    String registrationId = sharedPreferences.getString(Constants.SHARED_PREF_KEY_GCM_REGID, null);
    int currentAppVersion = sharedPreferences.getInt(Constants.SHARED_PREF_KEY_APP_VERSION, 0);
    if (getAppVersion(context) != currentAppVersion){
      return null;
    }
    return registrationId;
  }

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




Java Source Code List

com.turkcell.curio.CurioClient.java
com.turkcell.curio.CurioRequestProcessor.java
com.turkcell.curio.DBRequestProcessor.java
com.turkcell.curio.ICurioResultListener.java
com.turkcell.curio.INetworkConnectivityChangeListener.java
com.turkcell.curio.model.OfflineRequest.java
com.turkcell.curio.model.OnlineRequest.java
com.turkcell.curio.model.Screen.java
com.turkcell.curio.utils.Constants.java
com.turkcell.curio.utils.CurioClientSettings.java
com.turkcell.curio.utils.CurioDBContract.java
com.turkcell.curio.utils.CurioDBHelper.java
com.turkcell.curio.utils.CurioLogger.java
com.turkcell.curio.utils.CurioUtil.java
com.turkcell.curio.utils.NetworkUtil.java
com.turkcell.curio.utils.ParameterLoader.java
com.turkcell.curio.utils.PushUtil.java
com.turkcell.curio.utils.UUIDGenerator.java
com.turkcell.curio.utils.VisitorCodeManager.java
com.turkcell.curiosample.BlankActivity.java
com.turkcell.curiosample.MainActivity.java
com.turkcell.curiosample.PushNotificationBroadcastReceiver.java
com.turkcell.curiosample.PushNotificationIntentService.java