Notifications

Nib sends macOS notifications through the UserNotifications framework: push one now, schedule it for later, add action buttons and text input, and handle what the user chose.

Class Description
Notification One notification: title, body, sound, actions
NotificationManager app.notifications: push, schedule, cancel and query
Sound & Actions NotificationSound, NotificationSoundName, NotificationAction, NotificationActionOption, TextInputNotificationAction

Dev mode and built apps

macOS delivers notifications to app bundles. Under nib run and python main.py there is no bundle, and the runtime falls back:

Call Under nib run In a built app
app.notify(), push() An AppleScript banner with the default or a named system sound, no actions A notification
schedule(), schedule_daily(), reschedule() Nothing is scheduled Scheduled
cancel_notification(), cancel_all_notifications() Nothing happens Cancelled
scheduled(), delivered() Empty results The scheduled or delivered notifications
request_permission() Always reports granted The system dialog, then the saved answer
on_action() Never called Called on a click or an action

Test notifications with nib build.

Sending a notification

app.notify() is the short form:

import nib

def main(app: nib.App):
    app.title = "Notify"
    app.icon = nib.SFSymbol("bell")

    app.build(
        nib.Button("Send Notification", on_click=lambda: app.notify("Hello!", "World"))
    )

nib.run(main)
app.notify(
    "Download Complete",
    body="report.pdf has been saved to Downloads",
    subtitle="File Manager",
    sound=True,
)

For sounds, actions or an id to cancel it later, build a Notification and push it:

notification = nib.Notification(
    title="Alert",
    body="Something happened",
    sound=nib.NotificationSound(name=nib.NotificationSoundName.custom("Glass")),
)
app.notifications.push(notification)

The built-in sound names are listed under NotificationSoundName.

Permission

macOS asks the user before it delivers notifications. Request permission early:

granted = app.notifications.request_permission()
print("Notifications enabled" if granted else "Notifications denied")

The first call shows the system dialog; later calls return the saved answer. A user who denied permission re-enables it in System Settings.

Scheduling

from datetime import date, datetime, timedelta

reminder = nib.Notification(title="Break Time", body="Take a break!")

# Once, in an hour
app.notifications.schedule(reminder, at=datetime.now() + timedelta(hours=1))

# Move it
app.notifications.reschedule(reminder, at=datetime.now() + timedelta(hours=2))

# Every day at 9:00
app.notifications.schedule_daily(reminder, from_time="09:00")

# Three times a day, spread between 8:00 and 18:00
app.notifications.schedule_daily(reminder, from_time="08:00", to_time="18:00", count=3)

# Every day for a week
app.notifications.schedule_daily(
    reminder,
    from_time="09:00",
    from_date=date.today(),
    to_date=date.today() + timedelta(days=7),
)

The limits of each call are on NotificationManager.

Actions

Actions are buttons on the notification. A text_input turns one into a reply field:

notification = nib.Notification(
    title="New Message",
    body="Alice: Hey, are you free for lunch?",
    actions=[
        nib.NotificationAction(
            id="reply",
            title="Reply",
            text_input=nib.TextInputNotificationAction(
                button_title="Send",
                placeholder="Type your reply...",
            ),
        ),
        nib.NotificationAction(id="mark_read", title="Mark as Read"),
        nib.NotificationAction(
            id="delete",
            title="Delete",
            options=[nib.NotificationActionOption.DESTRUCTIVE],
        ),
    ],
)
app.notifications.push(notification)

on_action registers a handler and returns a Subscription whose stop() removes it:

def handle_action(notification_id, action_id, user_text):
    if action_id == "reply" and user_text:
        print(f"User replied: {user_text}")
    elif action_id == "mark_read":
        print("Marked as read")

subscription = app.notifications.on_action(handle_action)

The handler receives:

Argument Value
notification_id The id of the notification
action_id The action's id; "default" when the user clicked the notification itself, "dismiss" when they closed it
user_text The text typed into a text_input action, else None

Cancelling and querying

app.notifications.cancel_notification(notification.id)
app.notifications.cancel_all_notifications()

for n in app.notifications.scheduled():
    print(f"Scheduled: {n.title} (id: {n.id})")
print(len(app.notifications.delivered()), "showing")

Critical alerts

Critical alert sounds (NotificationSoundName.DEFAULT_CRITICAL) require a special entitlement from Apple. Without it, the sound falls back to the default. Request the entitlement at developer.apple.com.