Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions aw_notify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""

import logging
import shutil
import subprocess
import sys
import threading
from collections import defaultdict
Expand Down Expand Up @@ -142,17 +144,61 @@ def to_hms(duration: timedelta) -> str:

def notify(title: str, msg: str):
"""send a notification to the user"""

global notifier
if notifier is None:
notifier = DesktopNotifier(
app_name="AW",
app_icon=f"file://{icon_path}",
notification_limit=10,
)

logger.info(f'Showing: "{title} - {msg}"')
notifier.send_sync(title=title, message=msg)

# Try terminal-notifier first on macOS
if sys.platform == "darwin":
if notify_terminal_notifier(title, msg):
return

# Fall back to desktop-notifier
try:
if notifier is None:
notifier = DesktopNotifier(
app_name="AW",
app_icon=f"file://{icon_path}",
notification_limit=10,
)
notifier.send_sync(title=title, message=msg)
return
except Exception as e:
logger.info(f"desktop-notifier not used: {e}")

# If all notification methods fail, log a warning
logger.warning("All notification methods failed")


def notify_terminal_notifier(title: str, msg: str) -> bool:
"""Send notification using terminal-notifier. Returns True if successful."""
if not shutil.which("terminal-notifier"):
return False
try:
result = subprocess.run(
[
"terminal-notifier",
"-title",
"ActivityWatch",
"-subtitle",
title,
"-message",
msg.strip("-").replace("- ", ", ").replace("\n", ""),
"-appIcon",
str(icon_path),
"-group",
title,
"-open",
"http://localhost:5600",
],
capture_output=True,
text=True,
check=True,
)
return result.returncode == 0
except Exception as e:
logger.warning(f"Failed to send notification using terminal-notifier: {e}")
return False


class CategoryAlert:
Expand Down
Loading