Back to project page droidBBpush.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project droidBBpush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.arg3.examples.droidbb; // ww w . j av a 2 s . c o m import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.text.TextUtils; import android.util.Log; import com.arg3.examples.droidbb.annotations.ForApplication; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Observable; import javax.inject.Inject; /** * Created by ryan on 2014-08-27. */ public class PushRegistrar extends Observable { private static final String REGISTER_URL = "http://your_server_base_url_here/api/register"; private static final String TAG = PushRegistrar.class.getName(); private static final String PROPERTY_APP_VERSION = "app_version"; private static final String PROPERTY_REG_ID = "registration_id"; boolean needToSendToBackend = true; Context context; @Inject PushRegistrar(@ForApplication Context context) { this.context = context; } public boolean needsToSendToBackend() { return needToSendToBackend; } /** * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP * or CCS to send messages to your app. Not needed for this demo since the * device sends upstream messages to a server that echoes back the message * using the 'from' address in the message. */ public void sendRegistrationIdToBackend() { final String token = getRegistrationId(); if (TextUtils.isEmpty(token)) { Log.d(TAG, "No push token to register"); return; } notifyObservers(token); // TODO: this is where you would send the registration id to your server HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(REGISTER_URL); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); if (BuildConfig.FLAVOR.equals("blackberry")) nameValuePairs.add(new BasicNameValuePair("type", "blackberry")); else nameValuePairs.add(new BasicNameValuePair("type", "android")); nameValuePairs.add(new BasicNameValuePair("token", token)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Log.d(TAG, EntityUtils.toString(response.getEntity())); needToSendToBackend = false; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 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 String getRegistrationId() { final SharedPreferences prefs = getGCMPreferences(); String registrationId = prefs.getString(PROPERTY_REG_ID, ""); if (registrationId.isEmpty()) { Log.i(TAG, "Registration not found."); return ""; } if (!BuildConfig.DEBUG) { // 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(); if (registeredVersion != currentVersion) { Log.i(TAG, "App version changed."); return ""; } } return registrationId; } /** * @return Application's version code from the {@code PackageManager}. */ private int getAppVersion() { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return packageInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { // should never happen throw new RuntimeException("Could not get package name: " + e); } } /** * @return Application's {@code SharedPreferences}. */ private SharedPreferences getGCMPreferences() { // how you store the regID in your app is up to you. return context.getSharedPreferences(getClass().getName(), Context.MODE_PRIVATE); } /** * Stores the registration ID and app versionCode in the application's * {@code SharedPreferences}. * * @param regId registration ID */ public void storeRegistrationId(String regId) { final SharedPreferences prefs = getGCMPreferences(); int appVersion = getAppVersion(); 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(); if (needToSendToBackend) { sendRegistrationIdToBackend(); } else { notifyObservers(regId); } } public void clearRegistrationId() { getGCMPreferences().edit().remove(PROPERTY_REG_ID).commit(); needToSendToBackend = true; } }