Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[firebase_messaging] Disable interception of notification to allow for local notification generation #7874

Closed
Minituff opened this issue Jan 15, 2022 Discussed in #6113 · 8 comments
Labels

Comments

@Minituff
Copy link

Discussed in #6113

Originally posted by Minituff May 11, 2021

Background

I am trying to create a chat app which delivers reliable notifications that includes action buttons such as Mark as Read and Reply which creates an input field.

Unfortunately the firebase_messaging plugin does not support advanced options like Action/Input buttons. So, to do this in Flutter, I am using a great package called AwesomeNotifications which allows for all these advanced features. For anyone unfamiliar, it is like a more advanced version of the Local Notifications plugin.
Using this plugin, I am able to handle the FirebaseMessaging.onBackgroundMessage(_fcmBackgroundHandler); callback from firebase_messaging and generate the notification with all the properties and actions that I like.

The Problem

If the notification includes the notification property then firebase_messaging will automatically display the notification and AwesomeNotifications will then display its notification--resulting in duplicate notifications.
However, removing the notification property turns it into a data-only notification which, according to the FlutterFire docs are low-priority`

As mentioned above, data only messages are classed as "low priority". Devices can throttle and ignore these messages if your application is in the background, terminated, or a variety of other conditions such as low battery or currently high CPU usage.

You should not rely on data only messages to be delivered, they should only be used to support your applications non-critical functionality, e.g. pre-fetching data so the next time the user opens your app the data is ready to be displayed and if the message never gets delivered then your app still functions and fetches data on open.

To help improve delivery, you can bump the priority of messages. Note; this does still not guarantee delivery.

I have also tested this with my own IOS and Android devices and I would not receive data-only messages unless the app was in the foreground or the last used app on IOS. Notifications would not come through if I was using another app, or it was swiped away from recents.

For a chat application, getting reliable messages is a priority and therefore I need to use the notification property to ensure delivery of my notifications. The issues:

  1. I cannot use a plugin like AwesomeNotifications or Local Notifications to create advanced functioning notifications because it will create duplicate notifications as long as the notification property is set. However, the notification property is required for reliable notifications.
  2. The firebase_messaging plugin does not support advanced features such as Action/Input buttons.

A greater explanation of (number 1):
firebase_messaging automatically intercepts notifications and displays them and I don't think there is a way to disable that. Per the docs below:

Messages can be handled via the onBackgroundMessage handler. When received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running.
and further
If your message is a notification one (includes a notification property), the Firebase SDKs will intercept this and display a visible notification to your users (assuming you have requested permission & the user has notifications enabled). Once displayed, the background handler will be executed (if provided).

The Feature Request

I would like to be able to disable the firebase_messaging automatic notification display while still having the onBackgroundMessage callback fire. This way I can send reliable notifications (with the notification property) and have them be displayed by the more advanced flutter tools.

If this is not possible, then I would love to see ActionButtons and Input fields come to the firebase_messaging plugin.

Additional Infromation

I created another issue here https://github.com/rafaelsetragni/awesome_notifications/issues/165

@darshankawar darshankawar added triage Issue is currently being triaged. plugin: messaging type: enhancement New feature or request and removed triage Issue is currently being triaged. labels Jan 18, 2022
@feduke-nukem
Copy link

Have you already found any solution? @Minituff

@Minituff
Copy link
Author

Have you already found any solution? @Minituff

@coccyx-cyst No I haven't yet. The latest update I've found on a solution is here:
https://github.com/rafaelsetragni/awesome_notifications/issues/165#issuecomment-1013559494
It looks like @rafaelsetragni is getting close to a solution in the release-0-7-0 branch.

@baotrankhtn
Copy link

Same issue

@HemantNegi
Copy link

Facing the same issue. I want to add some customisations to notifications but turns out we can not customise background notifications. And if we try to launch our own custom notification then there are duplicates as there is no way to disable default background notifications.

My ask is, please provide a flag to disable triggering of automatic background notifications and let user create his own.

@russellwheatley
Copy link
Member

Hi @Minituff, there is no way of changing the system behaviour, it will always display a notification is it is present on the payload. You can increase the "low priority" aspect to data only payloads by doing the following:

On Android, set the priority field to high.
On Apple (iOS & macOS), set the content-available field to true.

@rulefahd
Copy link

" If this is not possible, then I would love to see ActionButtons and Input fields come to the firebase_messaging plugin."
That would be optimal!

@iosephmagno
Copy link

iosephmagno commented Aug 8, 2022

@russellwheatley there would be a way. We should include mutable-content in payload and fcm should support it by coding this workaround https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications

Was thinking this might be related to our #9300. As reported in #9315 #9300, it seems that silent notifications wont be triggered on device if app is terminated AND app is not the last one used from user.

Apple allows to modify the content of a remote notification though above feature in order to let developers use “notification” instead of (or along with) “data” and do sensitive things like decrypting the payload.

@iosephmagno
Copy link

iosephmagno commented Aug 8, 2022

To recap issue is:

Silent notifications let developers manipulate the content before showing head ups. Unfortunately they are not reliable on iOS which deliver them with low priority
apns-priority:5
contentAvailable: true

The result is that they dont work well when app is terminated and they might not be triggered under many conditions even in foreground/background mode.

Remote notifications are always delivered with highest priority and triggered in all app states: foreground / background / terminated.

However, as of now, firebase_messaging displays them instantly and prevents developers from editing its content. This feature is key for chat apps which need to decrypt notifications’ payload before displaying it.

Solution is to support content-mutable:1 inside payload and let developers edit payload fields even for “notification”, not just “data”.
https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications

@firebase firebase locked and limited conversation to collaborators Aug 21, 2022
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants