-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problems with creating a NSUserNotificationCenterDelegate using declare_class!() #606
Comments
I think there's a PR up for that already, btw: h4llow3En/mac-notification-sys#51
Makes sense, though I think that might be your issue? I haven't used NSUserNotification myself, but from my testing it seems like the existing Maybe it's the
Hmm, that sounds problematic! Could you hook up a debugger and see where the crash occurs? I tried running it myself a few times, including using AddressSanitizer, and couldn't reproduce the crash. |
Yes but the PR uses the User Notification Framework, so I want to get it running in the old deprecated way.
I got it working on my M1 Macbook running on MacOS 14.2.1 so it seems to still be working. Did it send notifications for you when you tried my code? Because it should send a notification and crash (sometimes/immediately after)
Maybe I am doing something wrong here, but if I declare the delegate in the .m file, there are no problems, and I get my notification interactions. The problem starts if I go pure rust on the delegate and try to write the If you want to try, I created a branch: The thing that is troubling me is if I add this to the notification.m file, it works perfectly. I would like to not use some dirty callback solution. Also my previous solution resulted in a lot of memory leaks. @interface NotificationCenterDelegate: NSObject <NSUserNotificationCenterDelegate>
@end
@implementation NotificationCenterDelegate
// Implement the delegate method to handle the user's response to the notification
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
NSLog(@"Notification clicked");
[center removeDeliveredNotification:notification];
}
@end and add this: NotificationCenterDelegate *delegate = [[NotificationCenterDelegate alloc] init];
NSUserNotificationCenter* center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = delegate; Regarding the debugger:
|
After hours and hours I found the solution, and it was not due to this crate: notification_center.setDelegate(Some(ProtocolObject::from_ref(delegate.as_ref()))); I found a workaround using a static variable like so: unsafe fn get_delegate() -> &'static Id<RustNotificationDelegate> {
static mut DELEGATE: MaybeUninit<Id<RustNotificationDelegate>> = MaybeUninit::uninit();
static ONCE: Once = Once::new();
ONCE.call_once(|| {
DELEGATE.write(RustNotificationDelegate::new());
});
DELEGATE.assume_init_ref()
}
fn init() {
//...
let notification_center = NSUserNotificationCenter::defaultUserNotificationCenter();
let delegate = get_delegate();
notification_center.setDelegate(Some(ProtocolObject::from_ref(delegate.as_ref())));
} Thank you very much for your support and the library, as already said: it makes my live much easier! |
Yeah, delegates are usually only weakly retained, that's not something that An alternative to the static would be to use Glad you figured it out though! |
We also link to this documentation from the property setters themselves. This is a common point of confusion, see: - #585 - #606 - https://matrix.to/#/!SrJvHgAPHenBakQHSz:matrix.org/$OsgLE1-VtAZ5f383z_UoEpDyS7xDUk67xveA_zWvyZA?via=matrix.org
I am trying to port the mac-notificaiton-sys to objc2, to be able to respond on multiple notifications at the same time while not blocking the whole thread (as
mac-notification-sys
does)I have some problems declaring the delegate for
NSUserNotificationCenter
see in detail here.It seems to declare the class, but when interacting with a notification, callback does not get called. I have tried quite a lot, and tried to get inspired by multiple different sources (winit, etc.) but I can't figure it out :(.
When running my creation I sometimes get a segfault, sometimes nothing and one time I got this:
Might there be a problem with the
NSUserNotificationCenterDelegate
implementation since it is deprecated?I would also use the User Notifications framework, but it requires signing the binaries[1]. Therefore, I try to stick with the deprecated API, since Electron and many other are still using it.
If you want to try it just download it and run
btw thanks for this crate, it makes development with objc much easier (i've tried a lot now :D).
The text was updated successfully, but these errors were encountered: