pywinrt/python-winsdk

Help to add specific expiry time and not keep notification (toast) saved in notification area

fredericomattos opened this issue · 5 comments

With this code I can generate a basic notification:

from winsdk.windows.ui.notifications import ToastNotificationManager, ToastNotification, ToastTemplateType

def toast_notification(AppID, title, text):
    XML = ToastNotificationManager.get_template_content(ToastTemplateType.TOAST_TEXT02)
    t = XML.get_elements_by_tag_name("text")
    t[0].append_child(XML.create_text_node(title))
    t[1].append_child(XML.create_text_node(text))
    notifier = ToastNotificationManager.create_toast_notifier(AppID)
    notifier.show(ToastNotification(XML))

if __name__ == '__main__':
    toast_notification('Visual Studio Code','title', 'text')

But I am not able to understand how do I specify the expiration time. I try:

toaster = ToastNotification(XML)
toaster.expiration_time = 1
notifier.show(toaster)

and

toaster = ToastNotification(XML)
toaster.expiration_time = datetime.datetime.now() + datetime.timedelta(seconds=1)
notifier.show(toaster)

But it returns the same error on both:

toaster.expiration_time = 1
TypeError: expecting winrt::Windows::Foundation::DateTime

For example, I would like to set the notification (toast) time on the screen to only 1 second.

And also how do I prevent this notification from being saved in the notifications panel (because it has no use, it only has value to appear on the screen for me to see and just it)

Hi @dlech , first of all thank you for your attention! I followed your updates and saw the commit created according to this issue, does that mean the issue is resolved? If so, could you show me an example of what my code would look like to make it work? I tried again and the same error keeps continue.

Sorry for not knowing how module updates work here on GitHub.

Note:
Before a new attempt I did:
pip install winsdk --upgrade

dlech commented

The changes have only been made in the pywinrt.exe tool that generates the code. The winrt package has not been updated yet.

The changes have only been made in the pywinrt.exe tool that generates the code. The winrt package has not been updated yet.

Oh yes, I understand. Thank you very much for your patience!
I wish you a good week.

Hi @dlech now it's accepting datetime, but still the notification stays active on the screen for 7 seconds (standard time) instead of 1 second and it continues to be archived in the notification area, could you help me understand what I might be doing wrong? Thanks in advance for your help!

from winsdk.windows.ui.notifications import ToastNotificationManager, ToastNotification, ToastTemplateType
import datetime

def toast_notification(AppID, title, text):
    XML = ToastNotificationManager.get_template_content(ToastTemplateType.TOAST_TEXT02)
    t = XML.get_elements_by_tag_name("text")
    t[0].append_child(XML.create_text_node(title))
    t[1].append_child(XML.create_text_node(text))
    notifier = ToastNotificationManager.create_toast_notifier(AppID)
    toaster = ToastNotification(XML)
    toaster.expiration_time = datetime.datetime.now() + datetime.timedelta(seconds=1)
    notifier.show(toaster)

if __name__ == '__main__':
    toast_notification('Visual Studio Code','title', 'text')
dlech commented

The docs for the expiration time say;

For Windows Phone 8.x app: this property also causes the toast notification to be removed from the action center once the specified date and time is reached.

So I think it only applies to the action center and not the popup. I can confirm that without setting the expiration time that the notification stays in the action center indefinitely, but with the expiration time it is removed from the action center after some time.

The actual duration does not seem to exactly match, but it is close. So there could be some mistake in the implementation of the conversion from Python timedelta to WinRTTimeSpan or it could just be a Windows quirk.


Also, a tip for using datetime with winrt APIs in general: be sure to always use "aware" times with a time zone (preferably UTC) specified, i.e. datetime.now(timezone.utc).