(initial version) |
m (→Header: Section rename) |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
'''CPDistributedNotificationCenter''' is a class that provides a subset of the functionality found in NSDistributedNotificationCenter on Mac OS X. It allows a process to send [[notifications]] globally without knowledge of listening clients. Interested clients can register for distributed notifications from a known server, and these notifications will be posted via the local NSNotificationCenter. | |||
For using CPDistributedNotificationCenter on iOS 7, see [[Updating extensions for iOS 7#Inter-process communication]]. | |||
== Usage == | == Usage == | ||
A server cannot immediately start posting notifications on creation. Even if a client process starts listening for notifications before the server runs, there is a minor delay incurred due to registration. Notifications sent by the server will only be sent to clients that have resulted in CPDistributedNotificationCenterClientDidStartListeningNotification being posted. A server can optionally watch for this notification (as well as CPDistributedNotificationCenterClientDidStopListeningNotification) and maintain a counter of listening processes. If there are no listening processes, then the server can avoid or defer posting notifications. | |||
=== Server === | === Server === | ||
Line 14: | Line 16: | ||
// Center name must be unique, recommend using application identifier. | // Center name must be unique, recommend using application identifier. | ||
CPDistributedNotificationCenter* notificationCenter; | CPDistributedNotificationCenter* notificationCenter; | ||
notificationCenter = [ | notificationCenter = [CPDistributedNotificationCenter centerNamed:@"unique.name.for.messaging.center.note"]; | ||
[notificationCenter runServer]; // or runServerOnCurrentThread on iOS 4.x or earlier | [notificationCenter runServer]; // or runServerOnCurrentThread on iOS 4.x or earlier | ||
[notificationCenter retain]; | [notificationCenter retain]; | ||
Line 21: | Line 23: | ||
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | ||
[nc addObserver:self | [nc addObserver:self | ||
selector:@selector(clientDidStartListening:) | |||
name:@"CPDistributedNotificationCenterClientDidStartListeningNotification" | |||
object:notificationCenter]; | |||
... | ... | ||
Line 30: | Line 32: | ||
- (void)clientDidStartListening:(NSNotification*)note | - (void)clientDidStartListening:(NSNotification*)note | ||
{ | { | ||
NSDictionary* userInfo = [note userInfo]; | |||
CPDistributedNotificationCenter* notificationCenter = [note object]; | |||
NSString* bundleIdentifier = [userInfo objectForKey:@"CPBundleIdentifier"]; | |||
[notificationCenter postNotificationName:@"notification name" | |||
userInfo:[NSDictionary dictionaryWithObject:@"hello" forKey:@"message"]]; | |||
// optionally call with toBundleIdentifier: to only send notification to this client (or any specific client) | // optionally call with toBundleIdentifier: to only send notification to this client (or any specific client) | ||
} | } | ||
Line 44: | Line 46: | ||
CPDistributedNotificationCenter* notificationCenter; | CPDistributedNotificationCenter* notificationCenter; | ||
notificationCenter = [CPDistributedNotificationCenter centerNamed:@"unique.name.for.messaging.center.note"]; | notificationCenter = [CPDistributedNotificationCenter centerNamed:@"unique.name.for.messaging.center.note"]; | ||
[notificationCenter | [notificationCenter startDeliveringNotificationsToMainThread]; | ||
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | ||
[nc addObserver:self | [nc addObserver:self | ||
selector:@selector(gotNotification:) // this method will be called whenever the server posts a notification | |||
name:nil // or name of the notification you are interested in | |||
object:nil]; | |||
</source> | </source> | ||
== | == External links == | ||
{{ | {{IPFHeader|AppSupport}} |
Latest revision as of 16:13, 26 October 2014
CPDistributedNotificationCenter is a class that provides a subset of the functionality found in NSDistributedNotificationCenter on Mac OS X. It allows a process to send notifications globally without knowledge of listening clients. Interested clients can register for distributed notifications from a known server, and these notifications will be posted via the local NSNotificationCenter.
For using CPDistributedNotificationCenter on iOS 7, see Updating extensions for iOS 7#Inter-process communication.
Usage
A server cannot immediately start posting notifications on creation. Even if a client process starts listening for notifications before the server runs, there is a minor delay incurred due to registration. Notifications sent by the server will only be sent to clients that have resulted in CPDistributedNotificationCenterClientDidStartListeningNotification being posted. A server can optionally watch for this notification (as well as CPDistributedNotificationCenterClientDidStopListeningNotification) and maintain a counter of listening processes. If there are no listening processes, then the server can avoid or defer posting notifications.
Server
-(id)init...
{
...
// Center name must be unique, recommend using application identifier.
CPDistributedNotificationCenter* notificationCenter;
notificationCenter = [CPDistributedNotificationCenter centerNamed:@"unique.name.for.messaging.center.note"];
[notificationCenter runServer]; // or runServerOnCurrentThread on iOS 4.x or earlier
[notificationCenter retain];
// Register to get notified of listening clients
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(clientDidStartListening:)
name:@"CPDistributedNotificationCenterClientDidStartListeningNotification"
object:notificationCenter];
...
}
- (void)clientDidStartListening:(NSNotification*)note
{
NSDictionary* userInfo = [note userInfo];
CPDistributedNotificationCenter* notificationCenter = [note object];
NSString* bundleIdentifier = [userInfo objectForKey:@"CPBundleIdentifier"];
[notificationCenter postNotificationName:@"notification name"
userInfo:[NSDictionary dictionaryWithObject:@"hello" forKey:@"message"]];
// optionally call with toBundleIdentifier: to only send notification to this client (or any specific client)
}
Client
CPDistributedNotificationCenter* notificationCenter;
notificationCenter = [CPDistributedNotificationCenter centerNamed:@"unique.name.for.messaging.center.note"];
[notificationCenter startDeliveringNotificationsToMainThread];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(gotNotification:) // this method will be called whenever the server posts a notification
name:nil // or name of the notification you are interested in
object:nil];
External links
|