Python
[파이썬] 윈도우11 알림센터 내용 가져오기
떡잎몬
2023. 5. 1. 21:44
반응형
python - get windows 11 notification manager message
윈도우 11에서 알림센터에 접근해서 알림들의 내용을 가져오는 기능을 구현해보자.
스택오버플로에 검색해 보아도 윈도우10 에서만 동작하는 코드가 보이고 윈도우11에서 동작하는 코드는 찾을 수 없었다.
그래서 윈도우 10 버전의 코드에서 이것 저것 조합해서 결국 내용을 가져오는 데 성공했다.
import asyncio # 비동기 함수로 만들기 위한 asyncio 임포트
from winsdk.windows.ui.notifications.management import UserNotificationListener # 윈도우 11부터는 winrt 가 아닌 winsdk 모듈을 임포트 해줘야 함
async def get_noti():
listener = UserNotificationListener.current
await listener.request_access_async()
while True:
notifications = await listener.get_notifications_async(1)
for notification in notifications:
app_display_name = notification.app_info.display_info.display_name
if app_display_name != "알람 발생 윈도우 이름":
continue
visual = notification.notification.visual
bindings = visual.bindings
for binding in bindings:
print(binding.template)
if binding.template == 'ToastGeneric':
toast_binding_generic = binding
text_elements = toast_binding_generic.get_text_elements()
texts = [text_element.text for text_element in text_elements]
# texts[0] : 알람 발생 윈도우 이름
# texts[1] : 알람 내용
text = "\n".join(texts)
await asyncio.sleep(0.5)
notification.visual 을 사용하여 binding.template가 ToastGeneric 인 것의 text_elements 를 가져오면, 알림센터에 있는 알림창의 내용을 가져올 수 있다.
실제 발생한 알람 :
스크립트를 통해 가져온 내용 :
반응형