Skip to content

Instantly share code, notes, and snippets.

@burntcookie90
Created October 20, 2014 17:58
Show Gist options
  • Save burntcookie90/31fb6401aff3b0bd70dc to your computer and use it in GitHub Desktop.
Save burntcookie90/31fb6401aff3b0bd70dc to your computer and use it in GitHub Desktop.
Helper for gcm registration
public class GCMRegistration {
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private String mSenderId;
private Activity mActivity;
private GoogleCloudMessaging mGCM;
private String mRegId;
public GCMRegistration(Activity activity) {
mActivity = activity;
}
public GCMRegistration(String senderId, Activity activity) {
mSenderId = senderId;
mActivity = activity;
}
public String getSenderId() {
return mSenderId;
}
public void setSenderId(String senderId) {
mSenderId = senderId;
}
/**
* 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.
*/
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mActivity);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, mActivity, PLAY_SERVICES_RESOLUTION_REQUEST).show();
}
else {
}
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.
*/
private String getRegistrationId() {
final LocalDataManager localDataManager = LocalDataManager.getSharedInstance();
String registrationId = localDataManager.getGcmRegId();
if (registrationId.isEmpty()) {
return "";
}
return registrationId;
}
public void register(Helper.Callbacks callbacks) {
if (checkPlayServices()) {
mGCM = GoogleCloudMessaging.getInstance(mActivity);
mRegId = getRegistrationId();
if (TextUtils.isEmpty(mRegId)) {
registerInBackground(callbacks);
}
}
else {
callbacks.onRegistrationComplete(false);
}
}
private void registerInBackground(final Helper.Callbacks callbacks) {
if (mActivity == null || TextUtils.isEmpty(mSenderId)) {
callbacks.onRegistrationComplete(false);
return;
}
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
String msg = "";
try {
if (mGCM == null) {
mGCM = GoogleCloudMessaging.getInstance(mActivity);
}
mRegId = mGCM.register(mSenderId);
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
// Persist the regID - no need to register again.
storeRegistrationId(mRegId);
return true;
} catch (IOException ex) {
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
return false;
}
}
@Override
protected void onPostExecute(Boolean success) {
callbacks.onRegistrationComplete(success);
}
}.execute(null, null, null);
}
private void sendRegistrationIdToBackend() {
}
private void storeRegistrationId(String regId) { }
public static class Helper {
GCMRegistration mGCMRegistrationHelper;
public Helper(Activity activity) {
mGCMRegistrationHelper = new GCMRegistration(activity);
}
public static Helper fromActivity(Activity activity) {
return new Helper(activity);
}
public Helper withSenderId(String senderId) {
mGCMRegistrationHelper.setSenderId(senderId);
return this;
}
public void register(Callbacks callbacks) {
mGCMRegistrationHelper.register(callbacks != null ? callbacks
: new Callbacks() {
@Override
public void onRegistrationComplete(boolean success) {
}
});
}
public interface Callbacks {
void onRegistrationComplete(boolean success);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment