Created
December 13, 2017 18:41
-
-
Save shawnthye/6da89593231c65e052b39163b154d772 to your computer and use it in GitHub Desktop.
Push Notification - Local Broadcast Receiver and Result Receiver
This file contains 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 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); | |
} | |
} |
This file contains 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 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"); | |
} | |
} | |
} |
This file contains 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 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