h4llow3En / mac-notification-sys

✉️ A simple wrapper to deliver or schedule macOS Notifications in Rust
98 stars 30 forks source link

set_application doesn't seem to take effect (and can't be set twice) #30

Open woj-tek opened 3 years ago

woj-tek commented 3 years ago

First, the source:

use mac_notification_sys::*;

fn main() {
    let bundle = get_bundle_identifier_or_default("reminder");
    set_application(&bundle).unwrap();

    let _ = send_notification("Title", &Some("Subtitle"), "This is the body", &None).unwrap();

    let other_app_bundle = get_bundle_identifier_or_default("other_app");
    set_application(&other_app_bundle).unwrap();

    send_notification(
        "Danger",
        &Some("Will Robinson"),
        "Run away as fast as you can",
        &None,
    )
    .unwrap();
}

There are two issues here: 1) the bundle doesn't seem to be set (notification still comes from "terminal", which is later on confirmed by (2) 2) it's not possible to set the application name twice (or, to have notification not override themselves, i.e. generate a couple of notifications that would be displayed at the same time):

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Application(AlreadySet("com.apple.Terminal"))', src/main.rs:10:40
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
hoodie commented 3 years ago

I agree this is not documented well at the moment. If I remember correctly this can only be set once, and therefore mac-notification-sys prevents you from setting it again. @h4llow3En didn't this result in a crash when setting the application more than once?

h4llow3En commented 3 years ago

This is correct. The the NSUserNotification Framework only throws an Exception if you want to change the bundle identifier when it has been set before.

woj-tek commented 3 years ago

OK, so we know the cause. But how do we then generate multiple notifications from the same bundle? Currently each call to send_notification() overrides previous notification (i.e. it's not possible to have 3 notifications displayed at the same time)

h4llow3En commented 3 years ago

The actual displaying of the notification is not part of this crate. Since all notifications are only scheduled in the Nofitications API (all notifications, not just from this crate) macOS itself handles those. You can schedule x notifications right after another but macOS itself delays them. At least in the NSNotifications framework there is no possibility to display two notifications in the same app simultaneously. But if I remember it correctly, no Application is allowed to do this.

The newer Notifications are displayed after the previous disappear.