Distributed notification no longer working

I

transfer data between a safari extension app and MacOS app by using Distributed Notification Center. I use

DistributedNotificationCenter.default().addObserver
with a specific notification name and I post a notification from the MacOS App using the same name
DistributedNotificationCenter.default().postNotificationName

I keep getting

attempt to post distributed notification 'nameofnotification' thwarted by sandboxing.

I saw in the apple documentation that

Sandboxed apps can send notifications only if they do not contain a dictionary. If the sending application is in an App Sandbox, userInfo must be nil.

My user Info is not nil, but this worked perfectly on previous versions of Mac. What am I missing ? Is this relatively new ?

What’s the alternative to communicate between extension and app? Would an XPC service work ?

Replies

Kinda janky but you can still send notifications if the CFDictionaryRef is nil

Code Block
CFNotificationCenterPostNotification(CFNotificationCenterRef center, CFNotificationName name, const void *object, CFDictionaryRef userInfo, Boolean deliverImmediately)


So you can convert the userInfo dict into a string

Code Block
NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&err];
NSString * myString = [[NSString alloc] initWithData:jsonData  encoding:NSUTF8StringEncoding];
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
CFNotificationCenterPostNotification(center, CFSTR("myNotification"), (__bridge const void *)(myString), nil, true);


Then in the receiving app convert the string back to a dict

Code Block
void notificationCallback (CFNotificationCenterRef center, void * observer, CFStringRef name, const void * object, CFDictionaryRef userInfo) {
NSString * strObject = (__bridge NSString *)object;
  NSData * data = [strObject dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * response;
if (data != nil) {
    response = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}


Not sure of the Swift implementation but it should work.
kind'a beats the purpose of sandboxing? I would guess there's some special "entitlement" you can apply to the sandboxed app to allow sending data to your App, however - you are of course aware that ANY observer can register for, and receive these notifications, together with the data you package there - so this can infringe user privacy. An XPC connection is preferable, becasues
  • The "XPC service" can verify incoming connection requests and deny based on their code-signing and other identity.

  • XPC calls can return a response, thus you can know that your notification passed through.