MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.44k stars 1.39k forks source link

Support for custom notification actions #17

Closed MaikuB closed 1 year ago

MaikuB commented 6 years ago

The plugin should provide the ability to specify custom notification actions. However, this will depends on the Flutter engine being able to support headless-Dart code as per https://github.com/flutter/flutter/issues/6192 and https://github.com/flutter/flutter/issues/3671

It appears Android support is there but will wait to see on if the engine can support it for iOS applications, and for Flutter to provide abstractions to access the functionality. Without the support being added in, then this won't work for scenarios like when the application has been terminated as the logic associated with the action would've been defined in Dart.

Update with remaining work (IMO)

Note that last two perhaps could be omitted given market share and those require using deprecated APIs

Edit:

MaikuB commented 4 years ago

Thanks for the PR @noinskit. Haven't had a chance to to take a look the PR yet but, I suspect it would still have the same problem with isolates that I reported before as the issue is still open

noinskit commented 4 years ago

@MaikuB I've seen that issue, but I think I'm not hitting it because I'm not using the same platform channel across more than one isolate (or maybe I misunderstood that issue's details).

anoopeashok commented 4 years ago

Fair enough. i've managed to at least handle it so that when app is terminated when comes to tapping the notification and then triggering a callback after launch. Anyway, appreciate you trying to provide help :)

how did you do that ?

YannBrrd commented 4 years ago

Hello,

flutter_background_fecth should do the trick AFAIU. Check this.

dluksza commented 4 years ago

Maybe a side note, some time ago I've implemented an Android Home Screen Widget with actions that works when flutter app is terminated.

The way I've achieved that is with dart isolates and shared_preferences. When app is starting it sets a shared preference value with dart isolate callback ID. Then when user taps on the Home Screen Widget, intent is emitted and handled on the native side. Intent handler reads isolate callback ID from shared preferences and launches the isolate, adding parameters to detect what action actually was fired from widget.

Then on the Flutter/Dart side, in the isolate handler I'm initializing get_it, recognize user action and execute rest off actions handlers.

This should also work for notification actions. If I find some time I'll crate a PR with POC, but if anyone else would like to try I'm happy to help.

Hope this helps.

noinskit commented 4 years ago

@dluksza you're right, this is what https://github.com/MaikuB/flutter_local_notifications/pull/338 is doing as well.

dluksza commented 4 years ago

@noinskit that's a pity that #338 was closed. I see the limitations. In my case I use firebase and am able to update data from isolate, then snapshots callback is called on the main app. But can imagine that this won't work for everyone. One can still use SendPort to communicate between isolate and main thread, but this complicate things :| I do like that you also implemented iOS part (can't say if it works) ;).

MaikuB commented 4 years ago

The issue I saw that is logged with the Flutter team (see original post) is that background execution could completely stop working. If you have some way to have it work in the background and main isolate whilst the app is running without such issues then I'd be happy to look at a pull request for it

dluksza commented 4 years ago

Well, I would start small. Just provide a way to execute dart code in response to notification action, explicitly document that it runs in separate isolate and can't modify UI unless some kind of external pub-sub service is used (like firestore).

I'm guessing 95% of time main app will not run when notification occurs. Also one can modify local data and start application from isolate using intents.

Having something that can add actions and react to them on dart side is already a huge step forward. It can be later improved, but it is better start with something that works for 70% of use cases, then not have this feature at all.

noinskit commented 4 years ago

Yeah, that PR I referred to has limitations and is essentially the "start small" approach. Instead of Firestore is uses shared_preferences (for data) and SendPort (for immediate notifications, if the main part is running).

AbdulMalikDev commented 4 years ago

Please add buttons to notifications, im using your plugin in my app and i need it.

bagintz commented 4 years ago

@MaikuB it seems the OneSignal flutter package allows for action buttons and (at least in terms of my very brief test of swiping from recents) it will show notification even if the app is terminated. Do you know of any reason flutter_local_notifications couldn't do the same? I am sure it is not that simple but I am wondering what OneSignal might have done to figure it out???

MaikuB commented 4 years ago

@bagintz OneSignal is for push notifications not local notifications so it's valid comparison due to different use cases and it works differently as well. From what I've seen, the only thing the buttons will do is launch the app. There was a feature request to allow for running code in the background that I don't believe has been implemented

bagintz commented 4 years ago

@MaikuB a feature request in the OneSignal package or flutter_local_notifications? The buttons only do launch the app but they pass in different values providing for a psuedo multiple action scenario (which is what I need, but I understand that is only one use case). I was more posting the observation about OneSignal in reply to your Feb 19th post:

The issue I saw that is logged with the Flutter team (see original post) is that background execution could completely stop working. If you have some way to have it work in the background and main isolate whilst the app is running without such issues then I'd be happy to look at a pull request for it

It would seem that the OneSignal team found some way around that (?)

MaikuB commented 4 years ago

a feature request in the OneSignal package or flutter_local_notifications?

OneSignal

It would seem that the OneSignal team found some way around that

I haven't seen anything to indicate that and I'm not sure what you are basing that on

bagintz commented 4 years ago

@MaikuB Great, I will look for that on the OneSignal repo.

As for the other I guess maybe I am not sure what the definition of "terminated" is for background execution. With the Flutter package for OneSignal I can get notifications with multiple action buttons if the app is swiped away, terminated from ADB and upon reboot (with the app not in recents). Would these cases qualify as "background execution could completely stop working" or is something else I am not thinking of. I am late to this conversation so I realize I may be missing something. Thanks!!

MaikuB commented 4 years ago

@bagintz yes but buttons should work to invoke code regardless of which state the app is in.

A reminder that an issue has been raised with the Flutter team where they have acknowledged there's are issues/limitations. So if you're doing any digging then I don't think there's much point until there's a solution that the Flutter team can come up with.

If you have need for things like custom action buttons, upvote on the issue linked in the last paragraph of my original post as it doesn't have many votes compared the reactions I see on this issue about adding notification buttons

AresVampire commented 4 years ago

local_notifications SDK provides to add actions to the notification. Can't it be done on flutter_local_notifications? Custom notification actions are widely used in mobile apps. https://pub.dev/documentation/local_notifications/latest/

Mohammad-ezzidden commented 4 years ago

local_notifications SDK provides to add actions to the notification. Can't it be done on flutter_local_notifications? Custom notification actions are widely used in mobile apps. https://pub.dev/documentation/local_notifications/latest/

it is only work if the app not TERMINITED.

iofjuupasli commented 4 years ago

Now it's possible to launch app by clicking on notification. Maybe it would be possible to do same for actions? But when opening with action, it will provide some payload to main app? It won't allow to handle actions in background, without launching app. However for a lot of cases it will be acceptable. As I understood from this issue, it's probably easier to implement.

andzejsw commented 4 years ago

Is it possible to add actions to notification now in the middle of 2020?

AbdulMalikDev commented 4 years ago

Been waiting for this for 2 months

shashikantdurge commented 4 years ago

This feature was requested 2 yrs ago. Any updates would help us organise better

Mohammad-ezzidden commented 4 years ago

disappointing -_-

MaikuB commented 4 years ago

@shashikantdurge it wasn't a request that was put in, I put this in myself to track it. There are no more updates as the status is still the same where I mention in the original post to upvote on the related issue on the Flutter repo that blocks this

AbdulMalikDev commented 4 years ago

@shashikantdurge it wasn't a request that was put in, I put this in myself to track it. There are no more updates as the status is still the same where I mention in the original post to upvote on the related issue on the Flutter repo that blocks this

Is there absolutely ANYTHING we can do other than upvote the issue on the official repo?

just guide me. Any number to complain or any person to speak to? This has to be done man, its not good if we're ignored for 2 years.

MaikuB commented 4 years ago

@AbdulMalikDev You could look at resolving the limitation in the Flutter engine yourself if you can.

Other thing is you could also looking at writing the code to handle this yourself on the native side. Plugins don't always solve for all cases, in which case developers need to look at writing their own code. For example https://github.com/edsnider/localnotificationsplugin and https://github.com/thudugala/Plugin.LocalNotification are two Xamarin plugins that don't have support for actions.

Similarly, custom notification layouts aren't supported either as those need layouts to be built the standard iOS and Android way

ghenry commented 4 years ago

Could you quote me for the work to add this feature @MaikuB if it's possible?

We sponsored the creation of https://github.com/cloudwebrtc/dart-sip-ua and I'd like action buttons for our app so a user and Accept or Reject an incoming call. And also for messaging for use with https://github.com/vukoye/xmpp_dart

It's best it's added here and open source.

Thanks (ghenry@surevoip.co.uk)

ghenry commented 4 years ago

We're also users of your appauth plugin @MaikuB so it would be good to contribute back to you for that great project.

Thanks!

MaikuB commented 4 years ago

@ghenry based on what you've written, sounds like perhaps you haven't been following this issue properly. There are limitations with handling actions using just Dart code when the app isn't running. For your purposes you should look to either (1) fork as the starting point and add in the necessary code on the native side or (2) write the code to handle notifications from scratch

Edit: appreciate the sentiment in trying contribute back as not many try to do so

ghenry commented 4 years ago

@MaikuB Understood, but based on what you've just written can 1) be abstracted and handled by this plugin on the native side like you are doing?

MaikuB commented 4 years ago

@ghenry you could have the notification shown via Dart using a plugin/platform channel but have Java/kotlin code that processes the actions specifically for your application's purpose

ghenry commented 4 years ago

@ghenry you could have the notification shown via Dart using a plugin/platform channel but have Java/kotlin code that processes the actions specifically for your application's purpose

Probably the only way :(

AbdulMalikDev commented 4 years ago

https://pub.dev/packages/background_fetch found this package. Would this be helpful to run dart in the background for this use case?

matamune94 commented 4 years ago

@AbdulMalikDev Yes, background_fetch good working with flutter_local_notification.

itssidhere commented 4 years ago

@MaikuB Any updates on this?

AbdulMalikDev commented 3 years ago

Any updates?

ghenry commented 3 years ago

Maybe we just need a wiki example of how to roll your own? There's some code here for Android:

https://medium.com/@info_67212/firebase-push-notification-with-action-button-in-flutter-a841da348097

and how to get the intents:

https://muetsch.io/how-to-receive-sharing-intents-in-flutter.html https://flutter.dev/docs/get-started/flutter-for/android-devs#how-do-i-handle-incoming-intents-from-external-applications-in-flutter

The problem is, as @MaikuB explained further up is all this is done in the Android code. Not sure about iOS.

Or am I way off? I have offered to sponsored some type of abstraction - https://github.com/MaikuB/flutter_local_notifications/issues/17#issuecomment-660651376

MaikuB commented 3 years ago

@itssidhere @MaikuB I already mentioned the issue that this is blocked by that you can subscribe to and upvote. If there aren't updates there then you're not going to see updates here.

@ghenry if you need to roll your own then it's a matter of reading the docs for each platform's notification APIs

asongkai commented 3 years ago

Any update on this topic? I need to implement a call notification with action button too.

ghenry commented 3 years ago

Here's an example on native Android side

https://github.com/doneservices/flutter_callkeep/blob/01e683d16be6d6675cf678d85be683718382a96a/android/src/main/kotlin/co/doneservices/callkeep/CallKeep.kt#L399

https://github.com/doneservices/flutter_callkeep/wiki/Native-android-code-that-handles-incoming-calls

asongkai commented 3 years ago

That's not suit the desire requirements.

I only need a notification with action button (decline call or accept call) and time to keep the notification display on top of phone

I don't need an extra features just only these two buttons and when I click on the actions button it will perform any action only my app and hide the notifications that's it.

ghenry commented 3 years ago

Read the code example I pasted then :-)

asongkai commented 3 years ago

I just went through it but I still have difficulty on link action between native code and dart code. By the way did anyone has done implement custom action on this code https://github.com/doneservices/flutter_callkeep/wiki/Native-android-code-that-handles-incoming-calls

with Agora?

please share

guyluz11 commented 3 years ago

Just to make sure, this includes buttons like in the alarm clock app (without opening the message content)?

Alarm
asongkai commented 3 years ago

Just to make sure, this includes buttons like in the alarm clock app (without opening the message content)?

Alarm

Could you share more details please.

hanyska commented 3 years ago

Any update?

guyluz11 commented 3 years ago

Just to make sure, this includes buttons like in the alarm clock app (without opening the message content)?

Alarm

Could you share more details please.

The notification will pop up with two buttons, one to snooze and one for dismiss. Ringtone will play in the background in the meantime, and it will stop only when pressing the dismiss button. Currently turning up the volume or pull from the top to see all notification stops the notification/ringtone sound. Additionally I need the buttons to signal my app so that it will do staff in the background for each action that was pressed. I am creating open source smart home and I want to link between people alarm clock to actions to execute in there home. In case the user pressed the snooze button it will not start any action and will "ask" him again later.

hanyska commented 3 years ago

Just to make sure, this includes buttons like in the alarm clock app (without opening the message content)?

Alarm

Could you share more details please.

The notification will pop up with two buttons, one to snooze and one for dismiss. Ringtone will play in the background in the meantime, and it will stop only when pressing the dismiss button. Currently turning up the volume or pull from the top to see all notification stops the notification/ringtone sound. Additionally I need the buttons to signal my app so that it will do staff in the background for each action that was pressed. I am creating open source smart home and I want to link between people alarm clock to actions to execute in there home. In case the user pressed the snooze button it will not start any action and will "ask" him again later.

Can you share notification code example? How did you add the buttons?

guyluz11 commented 3 years ago

@hanyska it is still not the functionality in my app, this is the functionality that I am trying to understand if the issue will add support for (at list part of it). The screen shot is example of the same behavior that currently can be found in google alarm clock.