-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: [flutter_local_notifications ] Notification actions for iOS, macOS, Android #880
Conversation
@MaikuB what code style does this project follow? Any particular setting I could enable/import in Android Studio? |
Awesome!
|
@ened I've done very little of pure native Android development so don't know how style settings could be configured so admittedly there isn't a particular style used at the moment. Only thing I try to do is avoid hungarian notification, naming constants with the FYI that there were other PRs before. The most comprehensive one was probably #338 as it included iOS support. There were also issues with background execution that I mentioned in #17 and reported on the Flutter repo. From memory, I saw discussions on the main Flutter repo that looking at background execution was on the horizon. Note: I say pure native Android development as my previous experience doing Android development was using Xamarin |
@MaikuB ok - thx for the link to the other PR & I will extract what is needed. The background execution support is included in Flutter, see flutter_workmanager or flutter_uploader plugins as a reference. I think we can get this sorted, but it will be a bit foreign to some developers, because the callback function runs in a separate thread. On the other hand, if you are using firebase_messaging and the For the code formatting, I normally enable the "google-java-formatter" plugin in Android studio and let it do it's magic on ALL java files. That's what the Flutter team is using as well. I can not include formatting changes in this PR though, so would you be willing to tackle this or should I submit a separate PR to reformat all of the Android java code using that plugin? Obviously it would include some notes in the README. |
@ened I know it's supported but I ran into what seemed like a rather severe issue when I tried to implement it in this plugin and within the code of one of the Flutter team member's own samples that I reported in flutter/flutter#23904. Note that I accidentally deleted the fork of the geofencing sample but it was acknowledged as an issue With regards to formatting, happy to take guidance on this and a PR on this. I have had to format Java code for PRs I've submitted to the Flutter and Firebase plugins that I would assume use the same formatter but used the script they had created for it as I wasn't aware that there's an Android Studio plugin for it |
Sorry to ask something unrelated to this thread, but the above caught my eye. I'm stuck on background messaging and thought it is exactly something to do with threading and my .Application class and .MainActivity class. Do you have a link to something I can read more about on what you've just highlighted? Thanks, |
The PR has been now been updated with a iOS implementation - from basic tests in emulator it works the same way as Android, but does require a a bit more preparation work by the user of this plugin:
I've updated the example project with this - but documentation is still a bit sparse; this is probably the biggest task now. It would be great if the community could support testing this and submit feedback. Will update the PR description with details. Also, we should define how much of the APIs available is to be implemented: https://developer.android.com/training/notify-user/build-notification#Actions @MaikuB For the code formatting, unfortunately google/google-java-format#536 is blocking a proper solution. Will follow this up when there are news. @ghenry I'm not sure what the problem is exactly. The example project in this fork uses the same mechanism, so I'd encourage you to test it locally. It's also a lot smaller than the firebase plugin, so should be easier to navigate around. :) |
@ghenry <https://github.com/ghenry> I'm not sure what the problem is
exactly. The example project in this fork uses the same mechanism, so I'd
encourage you to test it locally. It's also a lot smaller than the firebase
plugin, so should be easier to navigate around. :)
Thanks. After reading all the firebase code last night and callkeep code, I
think my issue is something being run in one isolate (background message
handler) and callkeep running in another isolate/activity with no access to
each other and callbacks in callkeep acting on empty states as the states
are in the other thread. Still researching. I may just bring the lot to the
foreground :)
Looking forward to playing with this code with GetX and xmpp_stone
Great work!
Gavin.
… |
Transporting messages across isolates could be done in two ways:
|
Thanks. After reading all the firebase code last night and callkeep code,
I think my issue is something being run in one isolate (background message
handler) and callkeep running in another isolate/activity with no access to
each other and callbacks in callkeep acting on empty states as the states
are in the other thread. Still researching. I may just bring the lot to the
foreground :) Looking forward to playing with this code with GetX and
xmpp_stone Great work! Gavin.
Transporting messages across isolates could be done in two ways:
1. You could try registering a named ReceivePort
<https://api.flutter.dev/flutter/dart-isolate/SendPort-class.html>
using IsolateNameServer
<https://api.flutter.dev/flutter/dart-ui/IsolateNameServer-class.html>.
2. You could use a plugin with a static field to keep/publish state
messages between the isolates. Having it done this way would allow you to
customize cache behavior when one of the isolates disappeared. This will
require both isolates to have access to plugins though.
Thanks Sebastian. I think I'll initialise some things in the background
message handler to test. That's enough of me polluting this issue, but
thanks for reading and replying.
Gavin.
|
Woa, awesome! This is exactly what I need in this plugin |
Hi, this is great! Exactly what I miss in this great plugin for long time! |
Glad to see this coming finally. I'll test and report... |
I need help for using this feature. The expected behavior of clicking an action button is similar to clicking the notification body except for an additional parameter which holds the clicked button id. However, in your solution, a function in a separate isolate is called and even the notification is not dismissed when the action button is clicked. (I tested it on Android) Is it not possible to trigger the Or is it possible to wake the application up in the |
Following answer is for the current implementation:
It's a good question whether the notification action should be dismissed automatically. I tend to agree it should be. Will look at implementing the code.
Precisely because it's in a separate isolate, the The handler has full app functionality available (plugins, etc), just not the App itself. This is a design decision in the Android system itself, as evidenced by how notification actions are handled (in a "BroadcastReceiver" which has a separate lifecycle).
Based on above, you could use plugins like There may be other issues by starting the activity this way, e.g. it will probably need to get a flag called "start new task", which means the App starts from 0 and you'd see the notification in a different place then |
The feature I've been waiting for so long, Is there any prediction about when it will be merged? |
@enesoren it needs more testing.. I can resolve issues but am currently very time-constrained rgd. testing. |
@ened Hi, this is great. any predictions, as to when will this get merged. |
@MadhaviMadineni the PR needs community testing, please see the instructions on the top. While at it, the plugin should probably also support arbitrary notification actions like text input (so that you can build a chat message app); will look at that next. |
Hi, I need help to test it. I get the following error when I make the change in pubspec: Could not find a file named "pubspec.yaml" in https://github.com/ened/flutter_local_notifications.git 6954ace. How can I solve this problem? |
@enesoren ah - I have written the wrong instructions on the top, please use this instead: dependency_overrides:
flutter_local_notifications:
git:
url: https://github.com/ened/flutter_local_notifications.git
path: flutter_local_notifications
ref: 17-notification-actions
flutter_local_notifications_platform_interface:
git:
url: https://github.com/ened/flutter_local_notifications.git
path: flutter_local_notifications_platform_interface
ref: 17-notification-actions |
@ened Yep, it works. I can test it now, thank you :) |
…al_notifications into 17-notification-actions
No idea yet - this seems to work well at the moment. I have not explored
Fixed!
Have added a basic version for this.
OK seems reasonable - will add to the todo list.
Yes I was hoping to get macOS support in as well, at least in the basic fashion. There are some limitations outlined in the code now. |
Thanks I'll see if it'll cooperate again on iOS for me to try this out. Is there a way for an action to trigger the app to be launched on Android though? I'd imagine that a launch intent should be used instead but then wonder how that would play out with callbacks. Not sure if that means a separate callback is required as the current one for notification actions would be for "background" actions |
Hi everyone subscribed. Please give this PR a try and support by reviewing code, writing tests and/or documentation. I think we are pretty close to a releasable version but I need your support. My time will be quite limited and I prefer not to keep this PR open for another year. ;-) |
@ened one option I can think of is to merge this in and do a pre-release. Would allow for changes to be consumed and tested more easily and reduce the need for you to deal with merge conflicts. This would also be a good basis for the plugin to have more refactoring done to create the Darwin classes for iOS and macOS. Thanks for your efforts on this To everyone else, I know there's a lot of subscribers on this but can I get a better indication of how many having been using @ened's fork so far? There are comments with questions or those describe issue but I wonder how many of you have successfully been using it and what feedback you have. If you could perhaps upvote on this message as indicator and optionally reply with feedback then that would be much appreciated |
Yes that sounds Good. You can mark the feature as experimental somewhere in the public API. And the next major version of the plug-in could remove the flag for example. |
I'll do that then :) |
I was using ened's fork in September 2021 but it was having compatibility issues with other plugins as this fork's dependencies were outdated then and assumed this wasn't being worked on anymore, so I copied ened's fork and updated things on my own and have been using it ever since. Anyway, I suspect it's all fixed now so I'll try this current version going forward. |
@AdamBridges thanks for the input. What kind of use cases did you have in your app and was the actions and the way they were handled in this PR sufficient to cover them? |
@lucasribolli late response but I suspect you might be confusing some things. An isolate is a concept specific to Flutter and a background isolate is spun when the user taps on action. That's not related to foreground services that is mentioned in the link. Furtherrmore, the plugin already supports foreground services. Clone the repository and run the example app on Android to take a look |
The use case included 3 actions that would either (1) store data to local/remote databases without opening the app; (2) open the app to a specific page; or (3) would reschedule the notification to fire again in 1-3 hours without opening the app. All of these actions work on Android except (2) and I had to resort to using an Android Intent package. |
@AdamBridges how did you handle (2) on iOS and application lifecycle i.e. navigating to a page when it app is running or terminated? |
@MaikuB I had issues with getting it to play nice on iOS so I used another package to handle notifications on iOS: The other package is capable of launching the app from an action and then there's logic in place to navigate the user from there. Messy, but it worked. I'll have an opportunity to test the notifications on iOS and Android with only this package in a couple weeks from now if you want to ask me about it again then. Otherwise, I'll try to remember to post my experiences then. |
I have been used this fork for some months and it seems to be good. But I have one issue to report that I cannot identify the cause. In iOS it seems not possible to use the own |
@AdamBridges @lucasribolli really appreciate you took the time and wrote some feedback. We can look into getting the component improved in the future. @MaikuB let's lock this conversation and add a new label/tag (perhaps issue template) for the issue tracker so that future bug reports can be found quickly. |
Yep makes sense. I've created a "notification actions" labels and will lock this. If you have some feedback or issue, relating to the functionality that's currently in the pre-release, please file a new issue with the label |
if (newCategories.count > 0) { | ||
UNUserNotificationCenter *center = | ||
[UNUserNotificationCenter currentNotificationCenter]; | ||
[center getNotificationCategoriesWithCompletionHandler:^( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ened I figured out what was the cause behind why notification actions stopped appearing for me on iOS and narrowed it to here. removing the logic to query the existing categories and then appending to that list allowed me to see the notification actions again. was there a reason for querying this? from my own tests and what i've seen in docs, should be able to just call [center setNotificationCategories:newCategories];
This PR addresses #17 - and includes a very basic implementation for iOS & Android.
A click on the notification will launch (if not already running) a separate Flutter engine, which initializes a callback dispatcher.
The dispatcher function then sets up platform communication for other plugins and starts listening to a event channel for existing or additional notification click events.
Screenshots
Android:
Simple actions:
Text input actions to collect user entered text:
iOS:
macOS:
Simple actions:
Text input actions to collect user entered text:
The PR is in early stage to gather feedback on the basic use case by others.
Feedback requested
Please support this PR by testing it in your own App and comment with missing features / use cases.
To use it, add the following to your
pubspec.yaml
file:The documentation is still very incomplete, therefore please refer to the example project of this branch.
Open tasks