Skip to content

Instantly share code, notes, and snippets.

@shawnthye
Created December 13, 2017 18:41
Show Gist options
  • Save shawnthye/6da89593231c65e052b39163b154d772 to your computer and use it in GitHub Desktop.
Save shawnthye/6da89593231c65e052b39163b154d772 to your computer and use it in GitHub Desktop.
Push Notification - Local Broadcast Receiver and Result Receiver
public class LocalBroadcastReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
Log.d("LocalBroadcastReceiver", "onReceive()");
// Tell the result receiver to CANCEL some specific action.
// eg. do not display System Notification
setResultCode(Activity.RESULT_CANCELED);
}
}
public class NotificationResultReceiver extends BroadcastReceiver {
private static final String TAG = "NotificationResult";
@Override public void onReceive(Context context, Intent intent) {
final int code = getResultCode();
if (code != Activity.RESULT_OK) {
// app is not active, as local/explicit broadcast receiver is not registered.
// generate System Notification
Log.d(TAG, "Activity.RESULT_CANCELED");
} else {
// LocalBroadcastReceiver registered.
// app is active OR on a specific screen you don't want to display System Notification,
Log.d(TAG, "Activity.RESULT_OK");
}
}
}
public void onReceiveFirebaseMessage() {
Intent intent = new Intent("your.package.intent.action.NOTIFICATION");
sendOrderedBroadcast(intent,
null,
new NotificationResultReceiver(),
null,
Activity.RESULT_OK,
null,
null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment