iOS上的私人AppSupport
框架有一个名为CPDistributedNotificationCenter
类,它似乎支持由NSDistributedNotificationCenter
在OS X提供的功能的子集如何接收通过CPDistributedNotificationCenter
我试图使用此类发布来自后台守护进程的通知,以便其他进程中的多个客户端可以接收这些通知并对其执行操作。我意识到还有其他选项,包括CPDistributedMessagingCenter
或CFMessagePort
,低级别马赫斯特端口,甚至达尔文的notify_post
。如果守护进程不知道客户端,我更喜欢它,并且我希望能够将数据和通知一起传递,并且notify_post
不允许这样做。
目前,这是我的守护进程正在做的:
CPDistributedNotificationCenter* center;
center = [CPDistributedNotificationCenter centerNamed:@"com.yfrancis.notiftest"];
[center runServer];
[center postNotificationName:@"hello"];
并在客户端:
CPDistributedNotificationCenter* center;
center = [CPDistributedNotificationCenter centerNamed:@"com.yfrancis.notiftest"];
[center startDeliveringNotificationsToMainThread];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:[Listener new]
selector:@selector(gotNotification:)
name:@"hello"
object:nil];
其中Listener
是实现一个方法gotNotification:
不幸的是,客户端永远不会收到'hello'通知。如果我用nil
替换addObserver
调用中的name
参数,我可以看到每个通知都发送到客户的通知中心,但'hello'不是其中的一个。
我通过查看SpringBoard
和CPDistributedNotificationCenter
的反汇编获得了我的代码的灵感。通知似乎通过CPDistributedNotificationCenter
的deliverNotification:userInfo:
递送,其作为NSNotificationCenter
的垫片postNotificationName:object:userInfo:
的垫片。
我在这里错过了什么?
XPC,其中每个进程可以保持与守护进程的双向连接?还是XPC,加上notify_post?一个缺点是XPC是iOS5 +。我认为XPC是图书馆的新标准,守护进程充当后端。 – conradev
我不希望守护进程有任何客户知识。 XPC击败了我,我只想发射一次性通知,如果有任何感兴趣的客户,他们可以采取行动。 – yfrancis
Activator使用注册机制,CPDistributedMessagingCenter需要远程中心名称来发送消息,因此需要客户端的知识 – yfrancis