diff --git a/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java b/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java index 480036643c7..27caf5f699f 100644 --- a/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java +++ b/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java @@ -145,6 +145,22 @@ public void onSendError(@NonNull String msgId, @NonNull Exception exception) {} public void onNewToken(@NonNull String token) {} ; + /** + * Called when this service is evaluating whether a given RemoteMessage should use standard + * behavior when it has a notification payload - that is, automatically show a Notification in the + * system tray and do not invoke {@link onMessageReceived} when the app is backgrounded. + * + *
A subclass can override this to return false for specific classes of messages if it wishes + * to suppress the default system tray behavior and handle display itself via onMessageReceived. + * + * @param message The messge object that has been parsed from an incoming push payload. + * @return True if default behavior should be used; false if no system tray Notification should be + * displayed and onMessageReceived should be invoked while backgrounded. + */ + public boolean shouldUseDefaultNotificationBehavior(@NonNull RemoteMessage message) { + return true; + } + /** @hide */ @Override protected Intent getStartCommandIntent(Intent originalIntent) { @@ -212,7 +228,8 @@ private void dispatchMessage(Intent intent) { // First remove any parameters that shouldn't be passed to the app // * The wakelock ID set by the WakefulBroadcastReceiver data.remove("androidx.content.wakelockid"); - if (NotificationParams.isNotification(data)) { + RemoteMessage message = new RemoteMessage(data); + if (shouldUseDefaultNotificationBehavior(message) && NotificationParams.isNotification(data)) { NotificationParams params = new NotificationParams(data); ExecutorService executor = FcmExecutors.newNetworkIOExecutor(); @@ -232,7 +249,7 @@ private void dispatchMessage(Intent intent) { MessagingAnalytics.logNotificationForeground(intent); } } - onMessageReceived(new RemoteMessage(data)); + onMessageReceived(message); } private boolean alreadyReceivedMessage(String messageId) {