Page 1 of 1

Working with CFNotificationCenter

Posted: Tue Mar 07, 2017 10:10 am
by wilbert
A distributed notification center delivers notifications between applications. A notification object used with a distributed notification center must always be a CFString object and the notification dictionary must contain only property list values.
Receiver example

Code: Select all

; >>> Constants and imports <<<

#CFNotificationSuspensionBehaviorDeliverImmediately = 4

ImportC "-framework CoreFoundation"
  CFNotificationCenterAddObserver(center, observer, callBack, name, object, suspensionBehavior)
  CFNotificationCenterGetDistributedCenter()
  CFNotificationCenterPostNotification(center, name, object, userInfo, deliverImmediately)
  CFRelease(cf)  
  CFStringCreateWithCString(alloc, cStr.p-utf8, encoding = $8000100)
EndImport



; >>> Receiver code <<<

ProcedureC Callback(center, observer, name, object, userInfo)
  Protected s.s
  If name And IsGadget(0)
    s = PeekS(CocoaMessage(0, name, "UTF8String"), -1, #PB_UTF8)
    AddGadgetItem(0, -1, s)
  EndIf
EndProcedure

distributedCenter = CFNotificationCenterGetDistributedCenter()
object = CFStringCreateWithCString(0, "notification.myApp")

CFNotificationCenterAddObserver(distributedCenter, #Null, @Callback(), #Null, object,
                                #CFNotificationSuspensionBehaviorDeliverImmediately)

OpenWindow(0, 0, 0, 320, 170, "Receiver", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 10, 10, 300, 150)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Sender example

Code: Select all

; >>> Constants and imports <<<

#CFNotificationSuspensionBehaviorDeliverImmediately = 4

ImportC "-framework CoreFoundation"
  CFNotificationCenterAddObserver(center, observer, callBack, name, object, suspensionBehavior)
  CFNotificationCenterGetDistributedCenter()
  CFNotificationCenterPostNotification(center, name, object, userInfo, deliverImmediately)
  CFRelease(cf)
  CFStringCreateWithCString(alloc, cStr.p-utf8, encoding = $8000100)
EndImport



; >>> Sender code <<<

distributedCenter = CFNotificationCenterGetDistributedCenter()
object = CFStringCreateWithCString(0, "notification.myApp")

name = CFStringCreateWithCString(0, "filename.txt")
CFNotificationCenterPostNotification(distributedCenter, name, object, #Null, #True)
CFRelease(name)

Re: Working with CFNotificationCenter

Posted: Fri Mar 10, 2017 5:08 pm
by mk-soft
Thanks :wink:

Re: Working with CFNotificationCenter

Posted: Sun Mar 12, 2017 4:28 am
by coder14
Thank you wilbert! Amazing work! :D :D :D

This must be added to the Mac forum stickies for future reference.

Re: Working with CFNotificationCenter

Posted: Wed Mar 15, 2023 4:07 pm
by jamirokwai
Hi Wilbert,

do you know how to subscribe to reachability? I just posted the question in the MacOS-specific forum, here:

Code: Select all

https://www.purebasic.fr/english/viewtopic.php?t=81108