Skip to content

Instantly share code, notes, and snippets.

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 AliAzaz/e6b1c64bc1b601e6ee6776e3b89a9cd9 to your computer and use it in GitHub Desktop.
Save AliAzaz/e6b1c64bc1b601e6ee6776e3b89a9cd9 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