public class LocalInstallation
extends java.lang.Object
SharedPreferences
to prevent duplicate registrations.
public class MyActivity extends Activity {
// This method must be called from onCreate() method every time
// the application starts. See the example Android application
// in loopback-push-notification for more details.
private void updateRegistration() {
final Context context = getApplicationContext();
// 1. Grab the shared RestAdapter instance.
final DemoApplication app = (DemoApplication) getApplication();
final RestAdapter adapter = app.getLoopBackAdapter();
// 2. Create LocalInstallation instance
final LocalInstallation installation =
new LocalInstallation(context, adapter);
// 3. Update Installation properties that were not pre-filled
// Enter the id of the application you registered in your LoopBack server
// by calling Application.register()
installation.setAppId(LOOPBACK_APP_ID);
// Substitute a real id of the user logged in this application
installation.setUserId("loopback-android");
// 4. Check if we have a valid GCM registration id
if (installation.getDeviceToken() != null) {
// 5a. We have a valid GCM token, all we need to do now
// is to save the installation to the server
saveInstallation(installation);
} else {
// 5b. We don't have a valid GCM token. Get one from GCM
// and save the installation afterwards.
registerInBackground(installation);
}
}
private void registerInBackground(final LocalInstallation installation) {
new AsyncTask<Void, Void, String>() {
//Override
protected String doInBackground(final Void... params) {
try {
final String regid = gcm.register(SENDER_ID);
installation.setDeviceToken(regid);
return "Device registered, registration ID=" + regid;
} catch (final IOException ex) {
Log.e(TAG, "GCM registration failed.", ex);
return "Cannot register with GCM:" + 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.
}
}
//Override
protected void onPostExecute(final String message) {
saveInstallation(installation);
// optionally log GCM registration result in `message`
}
}.execute(null, null, null);
}
void saveInstallation(final LocalInstallation installation) {
installation.save(new Model.Callback() {
//Override
public void onSuccess() {
// Log success
}
//Override
public void onError(final Throwable t) {
// Report error
}
});
}
}
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEVICE_TYPE_ANDROID |
static java.lang.String |
SHARED_PREFERENCES_NAME |
static java.lang.String |
STATUS_ACTIVE |
| Constructor and Description |
|---|
LocalInstallation(android.content.Context applicationContext,
RestAdapter loopbackAdapter)
Creates a new instance of LocalInstallation class.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getAppId() |
java.lang.String |
getAppVersion() |
java.lang.String |
getDeviceToken() |
java.lang.String |
getDeviceType() |
java.lang.Object |
getId() |
java.lang.String |
getStatus() |
java.lang.String[] |
getSubscriptions() |
java.lang.String |
getTimeZone() |
java.lang.String |
getUserId() |
void |
save(Model.Callback callback)
Saves the Installation to the remote server.
|
void |
setAppId(java.lang.String appId)
See
getAppId(). |
void |
setAppVersion(java.lang.String appVersion)
See
getAppVersion(). |
void |
setDeviceToken(java.lang.String deviceToken)
See
getDeviceToken(). |
void |
setStatus(java.lang.String status)
See
getStatus(). |
void |
setSubscriptions(java.lang.String[] subscriptions)
See
getSubscriptions(). |
void |
setTimeZone(java.lang.String timeZone)
See
getTimeZone() |
void |
setUserId(java.lang.String userId)
See
getUserId(). |
public static final java.lang.String DEVICE_TYPE_ANDROID
public static final java.lang.String STATUS_ACTIVE
public static final java.lang.String SHARED_PREFERENCES_NAME
public LocalInstallation(android.content.Context applicationContext,
RestAdapter loopbackAdapter)
applicationContext - The Android context of your application.loopbackAdapter - The adapter to use for communication with
the LoopBack server.public java.lang.Object getId()
public java.lang.String getAppId()
public void setAppId(java.lang.String appId)
getAppId().public java.lang.String getAppVersion()
public void setAppVersion(java.lang.String appVersion)
getAppVersion().public java.lang.String getUserId()
public void setUserId(java.lang.String userId)
getUserId().public java.lang.String getDeviceType()
DEVICE_TYPE_ANDROIDpublic java.lang.String getDeviceToken()
public void setDeviceToken(java.lang.String deviceToken)
getDeviceToken().public java.lang.String[] getSubscriptions()
public void setSubscriptions(java.lang.String[] subscriptions)
getSubscriptions().public java.lang.String getTimeZone()
public void setTimeZone(java.lang.String timeZone)
getTimeZone()public java.lang.String getStatus()
STATUS_ACTIVEpublic void setStatus(java.lang.String status)
getStatus().public void save(Model.Callback callback)
callback - The callback to be executed when finished.