Workaround (or fix maybe) for NEW_NOTIFICATION messages #12672
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt of a workaround (or fix) for #1964.
TL;DR
Incoming notifications are initially handled by system-level FCM code. Then an intent is passed to the application, that is partially handled by Firebase SDK code and partially by the application code in the
onMessageReceived
method.Sometimes (I have not been able to find when), the intent contains an additional flag that causes the Firebase SDK code to handle (display) the notification without invoking at all the
onMessageReceived
method defined in application code.As the unencrypted body of the notification message contains just the NEW_NOTIFICATION text, this is what gets shown to the user.
Code in this PR "intercepts" the incoming intent before the SDK code and clears the notification flag, so the SDK code always invokes the
onMessageReceived
method. The application code can then handle the message as usual - i.e. decrypt it and display proper notification to the user.More detailed description below
Firebase documentation is quite clear that notification messages are delivered to the application code only when the application is in foreground.
So it seems that notification messages should not work at all. But sometimes they do. 😉
I have not been able to find out, why in some cases incoming messages are treated as combined notification/data messages and in other cases as just data messages. This would probably need insight into inner working of the
push-notifications.nextcloud.com
server.I did find out however, that even in cases when the message is treated as a combined notification/data message, the application code is involved in handling of the message.
After some investigation of the Firebase Android SDK, I have found that the decision to show notification or invoke the
onMessageReceived
method is made by theFirebaseMessagingService
class, in thedispatchMessage
method.Then, I found that the
NotificationParams.isNotification
method just checks for values of two extras in the intent sent by the FCM system code.So in this PR, I propose to override the
handleIntent
method and clear these two extras from the incoming intent before letting the Firebase SDK code process it. This means that the notifications will be always delivered to theonMessageReceived
method - as expected.I have been able to get the NEW_NOTIFICATION messages on the emulator - although I am not sure if I can provide a repeatable scenario.
With the additional debug code from this PR, but without clearing the extras in
handleIntent
, I get the wrong notification and the following sequence in the logs.When I clear the extras in
handleIntent
, I get the correct notification and the following logs.