Thanks

Code: Select all
notification = CocoaMessage(0,CocoaMessage(0,0,"NSUserNotification alloc"),"init")
CocoaMessage(0,notification,"setTitle:$",@"test")
CocoaMessage(0,notification,"setSubtitle:$",@"test2")
CocoaMessage(0,notification,"setInformativeText:$",@"test3")
notificationCenter = CocoaMessage(0,0,"NSUserNotificationCenter defaultUserNotificationCenter")
CocoaMessage(0,notificationCenter,"deliverNotification:",notification)
Code: Select all
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSUserNotificationCenter *userNotificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
userNotificationCenter.delegate = self;
}
Code: Select all
app = CocoaMessage(0,0,"NSApplication sharedApplication")
OpenWindow(0,0,0,0,0,"")
CocoaMessage(0,app,"hide:")
notificationCenter = CocoaMessage(0,0,"NSUserNotificationCenter defaultUserNotificationCenter")
notification = CocoaMessage(0,CocoaMessage(0,0,"NSUserNotification alloc"),"init")
CocoaMessage(0,notification,"setTitle:$",@"test")
CocoaMessage(0,notification,"setSubtitle:$",@"test2")
CocoaMessage(0,notification,"setInformativeText:$",@"test3")
CocoaMessage(0,notificationCenter,"deliverNotification:",notification)
Repeat
ev = WaitWindowEvent()
Until ev = #PB_Event_CloseWindow
How can i do that? Where to start?wilbert wrote:I think PB also uses applicationDidFinishLaunching so you might need to hook into this and still call the implementation PureBasic provides to prevent problems.
You need method swizzling but I'm no expert on thatdeseven wrote:How can i do that? Where to start?
Can you try if this does anything at all ?deseven wrote:How to add an observer for NSApplicationDidFinishLaunchingNotification then?
Code: Select all
Global notificationCenter = CocoaMessage(0, 0, "NSNotificationCenter defaultCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = object_getClass_(appDelegate)
ProcedureC DidFinishLaunching(obj, sel, notification)
Protected userInfo.i = CocoaMessage(0, notification, "userInfo")
Protected userNotification.i = CocoaMessage(0, userInfo, "objectForKey:$", @"NSApplicationLaunchUserNotificationKey")
If userNotification
MessageRequester("UserNotification", "UserNotification was clicked")
EndIf
EndProcedure
selector = sel_registerName_("MyAppDidFinishLaunching:")
class_addMethod_(delegateClass, selector, @DidFinishLaunching(), "v@:@")
CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSApplicationDidFinishLaunchingNotification", "object:", #nil)
If OpenWindow(0, 0, 0, 320, 170, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
EditorGadget(0, 10, 10, 300, 150)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSApplicationDidFinishLaunchingNotification", "object:", #nil)
Code: Select all
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
I didn't read the last line carefully.NSUserNotificationCenterDelegate Protocol Reference wrote:To take an action when your application is launched as a result of a user clicking on a notification, be sure to implement the applicationDidFinishLaunching: method in the application class that implements the NSApplicationDelegate Protocol protocol. The notification parameter to that method has a userInfo dictionary, and if that dictionary has the NSApplicationLaunchUserNotificationKey key. The value of that key is the NSUserNotification object that caused the application to launch. The NSUserNotification object is delivered to the NSApplication delegate because that message will be sent before your application has a chance to set a delegate for the NSUserNotificationCenter.
Code: Select all
ProcedureC DidActivateNotification(obj, sel, center, notification)
MessageRequester("","didActivateNotification")
EndProcedure
ProcedureC ShouldPresentNotification(obj, sel, center, notification)
ProcedureReturn #YES
EndProcedure
; >>> Create delegate class <<<
delegateClass = objc_allocateClassPair_(objc_getClass_("NSObject"), "myDelegateClass", 0)
; >>> Add delegate methods <<<
class_addMethod_(delegateClass,
sel_registerName_("userNotificationCenter:didActivateNotification:"),
@DidActivateNotification(), "v@:@@")
class_addMethod_(delegateClass,
sel_registerName_("userNotificationCenter:shouldPresentNotification:"),
@ShouldPresentNotification(), "c@:@@")
; >>> Register class <<<
objc_registerClassPair_(delegateClass)
; >>> Create delegate instance <<<
delegate = class_createInstance_(delegateClass, 0)
Global userNotificationCenter = CocoaMessage(0, 0, "NSUserNotificationCenter defaultUserNotificationCenter")
CocoaMessage(0, userNotificationCenter, "setDelegate:", delegate)
Works like a charm! Thank you very much!wilbert wrote:Try this
Didn't know, thanks again.wilbert wrote:By the way, new is a combination of alloc and init so you can also create your notification
notification = CocoaMessage(0,0,"NSUserNotification new")