Last active
November 17, 2015 20:19
-
-
Save chadxz/eca1ae85fa316faa15c0 to your computer and use it in GitHub Desktop.
Registering a push token with respoke-sdk-android
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
/** | |
* Sorry if there are some imports or bits missing that are needed | |
* to make sense of this. I'm copy/pasting from a working example and | |
* trying to tease out things that are not relevant to push notification | |
* registration. | |
*/ | |
import android.app.Fragment; | |
import android.app.FragmentManager; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.graphics.Typeface; | |
import android.os.AsyncTask; | |
import android.os.Handler; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.digium.respokesdk.Respoke; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
import com.google.android.gms.gcm.GoogleCloudMessaging; | |
public class StartupActivity extends AppCompatActivity { | |
private static boolean registered = false; | |
private static final String TAG = "StartupActivity"; | |
public static final String PROPERTY_REG_ID = "registration_id"; | |
private static final String GCM_SENDER_ID = "YOUR_GCM_SENDER_ID"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Only register for push notifications once | |
if (!registered) { | |
// Check device for Play Services APK. If check succeeds, proceed with GCM registration. | |
if (checkPlayServices()) { | |
gcm = GoogleCloudMessaging.getInstance(this); | |
regid = getRegistrationId(getApplicationContext()); | |
if (regid.isEmpty()) { | |
registerInBackground(); | |
} else { | |
// We already have a token, so just send it to the Respoke push server | |
Respoke.sharedInstance().registerPushToken(regid); | |
registered = true; | |
} | |
} else { | |
Log.i(TAG, "No valid Google Play Services APK found."); | |
} | |
} | |
} | |
/// GCM methods | |
/** | |
* 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(this); | |
if (resultCode != ConnectionResult.SUCCESS) { | |
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { | |
GooglePlayServicesUtil.getErrorDialog(resultCode, this, | |
PLAY_SERVICES_RESOLUTION_REQUEST).show(); | |
} else { | |
Log.i(TAG, "This device is not supported."); | |
finish(); | |
} | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Gets the current registration ID for application on GCM service. | |
* 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(Context context) { | |
final SharedPreferences prefs = getGCMPreferences(context); | |
String registrationId = prefs.getString(PROPERTY_REG_ID, ""); | |
if (registrationId.isEmpty()) { | |
Log.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) { | |
Log.i(TAG, "App version changed."); | |
return ""; | |
} | |
return registrationId; | |
} | |
/** | |
* Registers the application with GCM servers asynchronously. | |
* Stores the registration ID and app versionCode in the application's shared preferences. | |
*/ | |
private void registerInBackground() { | |
new AsyncTask<Object,String,Object>() { | |
@Override | |
protected Object doInBackground(Object[] objects) { | |
String msg = ""; | |
try { | |
if (gcm == null) { | |
gcm = GoogleCloudMessaging.getInstance(getApplicationContext()); | |
} | |
regid = gcm.register(GCM_SENDER_ID); | |
msg = "Device registered, registration ID=" + regid; | |
// 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. | |
Respoke.sharedInstance().registerPushToken(regid); | |
registered = true; | |
// Persist the regID - no need to register again. | |
storeRegistrationId(getApplicationContext(), regid); | |
} catch (IOException ex) { | |
msg = "Error :" + ex.getMessage(); | |
// 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 msg; | |
} | |
protected void onPostExecute(String msg) { | |
Log.d(TAG, msg); | |
} | |
}.execute(null, null, null); | |
} | |
/** | |
* 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); | |
Log.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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment