Working with CFNotificationCenter

Mac OSX specific forum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Working with CFNotificationCenter

Post 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)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Working with CFNotificationCenter

Post by mk-soft »

Thanks :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Working with CFNotificationCenter

Post by coder14 »

Thank you wilbert! Amazing work! :D :D :D

This must be added to the Mac forum stickies for future reference.
jamirokwai
Enthusiast
Enthusiast
Posts: 771
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Working with CFNotificationCenter

Post 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
Regards,
JamiroKwai
Post Reply