jithurjacob / Windows-10-Toast-Notifications

Python library to display Windows 10 Toast Notifications
MIT License
970 stars 168 forks source link

Added on-click callback feature #38

Open Charnelx opened 6 years ago

Charnelx commented 6 years ago

Hi. I added callback on click functionality discussed in this issue . Implementation followed this comment plus my own code snippet for decorator/handler.

How it works - just add to show_toast method argument called callback_on_click with callable as value.

teltmau5 commented 5 years ago

Great, this works perfectly for me!

JDogg1329 commented 5 years ago

Is this going to be merged?

@Charnelx could you give a bit more detail on this, sorry guys bit of a Python noob, I downloaded the changed file and replaced the old one in source, built from source (I think that worked correctly?) Just need some clarification on how to implement a click action to my toast notifications

Charnelx commented 5 years ago

@JDogg1329 , don't know about merge, but you can get this from my own repo if needed. On-click implementation is really easy - just pass callable (in this case function that doesn't receive any arguments) as value of show_toast method parameter called callback_on_click.

Example:

def say_hello():
    print('Hello!')

toast = ToastNotifier()
toast.show_toast( title="Notification", msg="Here comes the message",
                    icon_path=None, duration=5, threaded=False, callback_on_click=say_hello)

In this case, if notification is clicked, say_hello function will be called.

JDogg1329 commented 5 years ago

Thanks @Charnelx ! So is it possible to pass an argument to the callback_on_click method?

Charnelx commented 5 years ago

@JDogg1329 , nope, you can't pass the argument in a direct way...but you can do a trick like this:

toast.show_toast( title="Notification", msg="Here comes the message",
                    icon_path=None, duration=5, threaded=False, callback_on_click=lambda: say_hello(arg))
vineethpisvas1 commented 5 years ago

@Charnelx Bro I have been trying some random stuff for a long time. Thanks for your help. Now I can die peacefully 🤣

zvibazak commented 5 years ago

@Charnelx I noticed that if user won't click on the notification, after 10 seconds (in windows 10) the message will be moved to "Action Center" (the notification area on the left side of the screen), and there I couldn't handle the user's click action. Do you have any idea? I want a web page to be opened when user click on the notification.

Thanks

hwsamuel commented 5 years ago

I've installed win10toast via pip and it seems that's not the latest code version because the callback_on_click argument doesn't work. Error says "TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'"

umutinevi commented 5 years ago

I've installed win10toast via pip and it seems that's not the latest code version because the callback_on_click argument doesn't work. Error says "TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'"

same for me.

ghost commented 5 years ago

Does this still work? I tried replacing my init.py contents with the contents from your file @Charnelx but the script was full of errors and said the callback_on_click was not an argument I could use. Sorry if I am doing something wrong, but I'm a noob

Charnelx commented 4 years ago

@crappy-coder21 , just try to update your version using pip.

LeNarvalo commented 4 years ago

@Charnelx Nice work ! Are there any way to get win32con.WM_RBUTTONUP in lparam for example ?

Zhell1 commented 4 years ago

this should be merged to master it is a very useful feature

MPeek1995 commented 4 years ago

Anybody still having problems with the: TypeError: show_toast() got an unexpected keyword argument 'callback_on_click' I keep getting this message.

I installed yesterday so it must be the latest version.

What could i do wrong?

VerifyBot commented 4 years ago

Was it fixed? Because I still get the error TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'

JakubBlaha commented 4 years ago

Will this ever be merged? Because I hope so.

JakubBlaha commented 4 years ago

If anyone want's this feature and is using pipenv, the following command will update the package to the version with the callback_on_click parameter.

pipenv install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast

I couldn't get it to work using pip.

aleanun89 commented 4 years ago

LGTM

sercanyilmaz84 commented 4 years ago

There is still same problem:

TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'

malun22 commented 3 years ago

Here is the init: https://stackoverflow.com/questions/62828043/how-to-perform-a-function-when-toast-notification-is-clicked-in-python

frak0d commented 3 years ago

Is it ever going to merge ? Or is @jacobcolbert dead due to corona ?

Its been 2 Years Now !

@Charnelx Please make a New pip Repo with Your Version 🙏

leon-mueller commented 3 years ago

Hey Guys,

this works perfectly, BUT:

in my case the callback_on_click is executed instantly after this toast shows up.

How can i make that the action only executes if I click it?

cheers leon

jakubblaha-ids commented 3 years ago

@leon-mueller

You are probably doing something like

...(callback_on_click=print('clicked'))

when in reality, you wanna do

...(callback_on_click=lambda: print('clicked'))
leon-mueller commented 3 years ago

@jakubblaha-ids

I'm doing as followed:

... callback_on_click=open_ticket(ticket_link)

def open_ticket(url): webbrowser.open(url)

In this case it's always executing the def open_ticket when the toast shows up

JakubBlaha commented 3 years ago

@leon-mueller

What you are doing is essentially the same as...

result = open_ticket(ticket_link)
...(click_on_callback=result)

You are calling the function and assigning it's return value to the click_on_callback parameter.

You instead need to pass in the function (not it's call resulting in a return value) as the click_on_callback argument. This can be done by defining a function without a name using the lambda keyword (you can google that) as follows.

...(callback_on_click=lambda: open_ticket(ticket_link))
leon-mueller commented 3 years ago

@JakubBlaha

aaaah damn didn't thought about that tho. Thank you very much!!!!

jellytoaster commented 3 years ago

i feel dumb, but how do i install it????

JakubBlaha commented 3 years ago

@jellytoaster https://github.com/jithurjacob/Windows-10-Toast-Notifications/pull/38#issuecomment-633012502

jellytoaster commented 3 years ago

@jellytoaster #38 (comment) thx

jellytoaster commented 3 years ago

help, im getting errors saying "the system could not find the file specified." do i need anaconda or something????

JakubBlaha commented 3 years ago

@jellytoaster With this little information noone can help you. You will better off to ask on StackOverflow. GitHub issues is not a place to ask such questions.

ZhangTianrong commented 3 years ago

If anyone want's this feature and is using pipenv, the following command will update the package to the version with the callback_on_click parameter.

pipenv install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast

I couldn't get it to work using pip.

If you don't use pipenv, you can clone the repo and install through python .\setup.py install. If there is error, change attrgetter("req") in line 21 of setup.py to attrgetter("requirement") as indicated here.

Ariel-MN commented 3 years ago

Is there a fork that allows using duration = False?

coredev-uk commented 3 years ago

Is there a fork that allows using duration = False?

If you're aiming to keep the notification persistent in the action centre duration=None works if that's what you're going at having it false.

dariopnc commented 3 years ago

Is it ever going to merge ? Or is @jacobcolbert dead due to corona ?

Its been 2 Years Now !

@Charnelx Please make a New pip Repo with Your Version 🙏

I suppose you meant @jithurjacob instead of @jacobcolbert

frak0d commented 3 years ago

I suppose you meant @jithurjacob instead of @jacobcolbert

i believe he changed his name

leolencelAI commented 3 years ago

I've a problem, I ran this code but the function trigger as soon as the toast appear on the screen. So click don't work. Do you have the same problem? (I would like to open a webpage whether I click on the toast)

ZhangTianrong commented 3 years ago

I've a problem, I ran this code but the function trigger as soon as the toast appear on the screen. So click don't work. Do you have the same problem? (I would like to open a webpage whether I click on the toast)

That's because you used a function call as the trigger instead of the function itself.

Fakeaccount12312 commented 3 years ago

@Charnelx I noticed that if user won't click on the notification, after 10 seconds (in windows 10) the message will be moved to "Action Center" (the notification area on the left side of the screen), and there I couldn't handle the user's click action. Do you have any idea?

ZLB08 commented 3 years ago

The library works well for me except on one point: I want my notification to stay in the action center if I don't click on it. So I set the duration parameter to None. However, after 10s, the notification disappear from the action center.

It is strange because this functionality works well on the previous version (win10toast_persist). Does anyone had the same issue already ?

dmtzs commented 2 years ago

This pull request will be merged and able when you do your pip install someday?