Skip to content

Instantly share code, notes, and snippets.

@renaudmathieu
Last active January 18, 2016 14:09
Show Gist options
  • Save renaudmathieu/56a3f0a8a2f898fd81ae to your computer and use it in GitHub Desktop.
Save renaudmathieu/56a3f0a8a2f898fd81ae to your computer and use it in GitHub Desktop.
Using your own BroadcastReceiver with Localytics and any other Receiver
public class GCMBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = GCMBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) {
// Register Localytics (this will call Localytics.handleRegistration(intent))
new PushReceiver().onReceive(context, intent);
} else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
if (intent.getExtras().containsKey("ll")) {
// Send the notification to Localytics
new PushReceiver().onReceive(context, intent);
/*
Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time.
As each receiver executes in turn, it can propagate a result to the next receiver,
or it can completely abort the broadcast so that it won't be passed to other receivers.
The order receivers run in can be controlled with the android:priority attribute
of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.
*/
// Let's consume the intent for other Receiver
if (isOrderedBroadcast()) {
setResultCode(Activity.RESULT_CANCELED);
abortBroadcast();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment