Last active
December 30, 2015 05:39
-
-
Save beshkenadze/7783851 to your computer and use it in GitHub Desktop.
Thx @artem-zinnatullin for fixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.projectname.android; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.os.AsyncTask; | |
import android.support.v4.content.LocalBroadcastManager; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
import com.google.android.gms.gcm.GoogleCloudMessaging; | |
import com.projectname.android.BuildConfig; | |
import java.io.IOException; | |
/** | |
* Created by Aleksandr Beshkenadze <[email protected]> on 04.12.13. | |
*/ | |
public class GCMHelper { | |
public final static String ACTION_GCM_REGISTER = "com.google.android.c2dm.intent.action.ACTION_GCM_REGISTER"; | |
public final static String EXTRA_GCM_ID = "com.google.android.c2dm.intent.extra.EXTRA_GCM_ID"; | |
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; | |
public static final String EXTRA_MESSAGE = "message"; | |
public static final String PROPERTY_REG_ID = "registration_id"; | |
private static final String PROPERTY_APP_VERSION = "appVersion"; | |
public static final String SENDER_ID = BuildConfig.GCM_SENDER_ID; | |
private static final String TAG = "GCM"; | |
private static volatile GCMHelper instance; | |
private final Context context; | |
private GoogleCloudMessaging gcm; | |
private RegisterGCMAsyncTask registerGCMAsyncTask; | |
private GCMHelper(Context context) { | |
this.context = context; | |
} | |
public static GCMHelper getInstance(Context context) { | |
if (instance == null) { | |
synchronized (GCMHelper.class) { | |
if (instance == null) { | |
instance = new GCMHelper(context); | |
} | |
} | |
} | |
return instance; | |
} | |
/** | |
* Check the device to make sure it has the Google Play Services APK. If | |
* it doesn't, display a dialog that allows users to download the APK from | |
* the Google Play Store or enable it in the device's system settings. | |
*/ | |
public static boolean checkPlayServices(Activity activity) { | |
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity.getApplicationContext()); | |
if (resultCode != ConnectionResult.SUCCESS) { | |
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { | |
GooglePlayServicesUtil.getErrorDialog(resultCode, activity, | |
PLAY_SERVICES_RESOLUTION_REQUEST).show(); | |
} else { | |
L.i(TAG, "This device is not supported."); | |
} | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Gets the current registration ID for application on GCM service. | |
* <p/> | |
* If result is empty, the app needs to register. | |
* | |
* @return registration ID, or empty string if there is no existing | |
* registration ID. | |
*/ | |
public static String getRegistrationId(Context context) { | |
final SharedPreferences prefs = getGCMPreferences(context); | |
String registrationId = prefs.getString(PROPERTY_REG_ID, ""); | |
if (registrationId.isEmpty()) { | |
L.i(TAG, "Registration not found."); | |
return ""; | |
} | |
// Check if app was updated; if so, it must clear the registration ID | |
// since the existing regID is not guaranteed to work with the new | |
// app version. | |
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); | |
int currentVersion = getAppVersion(context); | |
if (registeredVersion != currentVersion) { | |
L.i(TAG, "App version changed."); | |
return ""; | |
} | |
return registrationId; | |
} | |
/** | |
* @return Application's {@code SharedPreferences}. | |
*/ | |
public static SharedPreferences getGCMPreferences(Context context) { | |
// This sample app persists the registration ID in shared preferences, but | |
// how you store the regID in your app is up to you. | |
return context.getSharedPreferences(context.getPackageName(), | |
Context.MODE_PRIVATE); | |
} | |
/** | |
* @return Application's version code from the {@code PackageManager}. | |
*/ | |
public static int getAppVersion(Context context) { | |
try { | |
PackageManager pm = context.getPackageManager(); | |
if (pm != null) { | |
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0); | |
return packageInfo.versionCode; | |
} | |
} catch (PackageManager.NameNotFoundException e) { | |
// should never happen | |
throw new RuntimeException("Could not get package name: " + e); | |
} | |
return -1; | |
} | |
public RegisterGCMAsyncTask registerGCMAsync() { | |
if (registerGCMAsyncTask == null) | |
registerGCMAsyncTask = (RegisterGCMAsyncTask) new RegisterGCMAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | |
return registerGCMAsyncTask; | |
} | |
public class RegisterGCMAsyncTask extends AsyncTask<Void, String, String> { | |
@Override | |
protected String doInBackground(Void... params) { | |
String regId = null; | |
try { | |
if (gcm == null) { | |
gcm = GoogleCloudMessaging.getInstance(context); | |
} | |
regId = gcm.register(SENDER_ID); | |
storeRegistrationId(context, regId); | |
sendBroadcast(context, regId); | |
} catch (IOException ex) { | |
L.e(TAG, ex.toString()); | |
} | |
return regId; | |
} | |
/** | |
* Stores the registration ID and app versionCode in the application's | |
* {@code SharedPreferences}. | |
* | |
* @param context application's context. | |
* @param regId registration ID | |
*/ | |
private void storeRegistrationId(Context context, String regId) { | |
final SharedPreferences prefs = getGCMPreferences(context); | |
int appVersion = getAppVersion(context); | |
L.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(); | |
} | |
@Override | |
protected void onPostExecute(String msg) { | |
super.onPostExecute(msg); | |
} | |
} | |
private void sendBroadcast(Context context, String regId) { | |
Intent broadcastIntent = new Intent(ACTION_GCM_REGISTER); | |
broadcastIntent.putExtra(EXTRA_GCM_ID, regId); | |
LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment