Last active
December 18, 2015 14:28
-
-
Save secondsun/5797051 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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(); | |
} | |
} |
This file contains hidden or 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
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(); | |
} |
This file contains hidden or 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
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