Skip to content

Instantly share code, notes, and snippets.

@secondsun
Last active December 18, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secondsun/5797051 to your computer and use it in GitHub Desktop.
Save secondsun/5797051 to your computer and use it in GitHub Desktop.
public class MyApplication extends Application {
public Registrar registrar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Registrar r = new Registrar(PUSH_REGISTRATION_URL);
PushConfig config = new PushConfig(GCM_APP_ID);
config.setMobileVariantId(AG_PUSH_VARIANT_ID);
r.register(getApplicationContext(), config, new Callback<Void>() {
});
}
public MyActivity extends Activity implements MessageHandler {
private MyApplication app;
public void onStart() {
app = ( MyApplication ) getApplication();
}
public void onResume() {
app.attachMessageHandlerOnMain(this);
}
public void onPause() {
app.detachMessageHandlerOnMain(this);
}
public void onMessage(Bundle message) {
Toast.makeToast(message.toString()).show();
}
}
public interface MessageHandler {
/**
* Handle a delete message. These are sent by Google to notify the applicaiton that messages
* have been removed on the GCM side and the app needs to do a full update.
*/
public void onDeleteMessage(Bundle message);
/**
* This code will be invoked when a message is received (which is not a delete message)
*/
public void onMessage(Bundle message);
public void onError();
}
public interface Registrar{
/**
* Registers this Installation with Unified push server
*/
public void register(final Context context, final PushConfig config,
final Callback<Void> callback);
/**
* Unregisters this Installation with Unified push server.
* Push notifications will not be received
*/
public void unregister(final Context context,
final Callback<Void> callback);
/**
* Messages received will be handled on the applications Main thread by the handler.
*/
public void attachMessageHandlerOnMain(MessageHandler handler);
/**
* Messages received will be handled on a background thread
*/
public void attachMessageHandlerOnBackground(MessageHandler handler);
/**
* Messages received will be handled on the applications Main thread by the handler.
*/
public void detachMessageHandlerOnMain(MessageHandler handler);
/**
* Messages received will be handled on a background thread
*/
public void detachMessageHandlerOnBackground(MessageHandler handler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment