mac-notification-sys icon indicating copy to clipboard operation
mac-notification-sys copied to clipboard

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

Open woj-tek opened this issue 4 years ago • 4 comments

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

woj-tek avatar Apr 11 '21 09:04 woj-tek

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?

hoodie avatar Apr 17 '21 12:04 hoodie

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.

h4llow3En avatar Apr 21 '21 06:04 h4llow3En

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)

woj-tek avatar Apr 21 '21 10:04 woj-tek

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.

h4llow3En avatar Apr 25 '21 13:04 h4llow3En