Skip to content

Instantly share code, notes, and snippets.

View CHLibrarian's full-sized avatar

ContextHub CHLibrarian

View GitHub Profile
@CHLibrarian
CHLibrarian / element-services-geofence-delete.mm
Created August 11, 2014 20:45
ContextHub Element Services Geofence Delete Gist
// Deleting a geofence takes the same NSDictionary structure as the response from creating, getting or updating one
[[CCHGeofenceService sharedInstance] deleteGeofence:geofence completionHandler:^(NSError *error) {
if (!error) {
NSLog(@"Deleted geofence in ContextHub");
// If you do not have push properly set up, you need to explicitly call synchronize on CCHSensorPipeline so it will start/stop generate events if it applies to this device
[[CCHSensorPipeline sharedInstance] synchronize:^(NSError *error) {
if (!error) {
@CHLibrarian
CHLibrarian / event-services-protocols.mm
Last active August 29, 2015 14:05
ContextHub Event Services Protocols Gist
// Set the data source and delegate to a class which will define the methods listed below
// Typically this is the application delegate but it can be whatever makes sense in your application
[[CCHSensorPipeline sharedInstance] setDataSource:self];
[[CCHSensorPipeline sharedInstance] setDelegate:self];
#pragma mark - Sensor Pipeline Data Source
- (NSDictionary *)sensorPipeline:(CCHSensorPipeline *)sensorPipeline payloadForEvent:(NSDictionary *)event {
// Add custom data structures to the events, and they will end up on the server.
return @{};
@CHLibrarian
CHLibrarian / event-services-add-subscription.mm
Last active August 29, 2015 14:05
ContextHub Event Services Add Event Subscription Gist
// Subscribe to "office-chaione" tag and start receiving events from beacon and geofence elements with that tag
NSString *tag = @"office-chaione";
if ([[CCHSensorPipeline sharedInstance] addElementsWithTags:@[tag]]) {
NSLog(@"Successfully added subscription to \"%@\" tag", tag);
} else {
NSLog(@"Failed to add subscription to \"%@\" tag", tag);
}
@CHLibrarian
CHLibrarian / event-services-observe-notification.mm
Last active August 29, 2015 14:05
ContextHub Event Services Observe Notification Gist
// Call method "handleEvent:" when an event will be posted
// This method is called before a network request is made
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEvent:)
name:CCHSensorPipelineWillPostEvent object:nil];
// Call method "handleEvent:" when an event has been posted (recommended)
// This method is made after a network request has been successful
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEvent:)
name:CCHSensorPipelineDidPostEvent object:nil];
@CHLibrarian
CHLibrarian / event-services-handle-notification.mm
Last active August 29, 2015 14:05
ContextHub Event Services Handle Notification Gist
- (void)handleEvent:(NSNotification *)notification {
NSDictionary *event = notification.object;
// Check if the event is a beacon event
if ([event valueForKeyPath:CCHBeaconEventKeyPath]) {
// Handle beacon event here
// The "DetectMe" sample app goes into greater detail of handling beacon events
}
// Check if event is a geofence event
@CHLibrarian
CHLibrarian / event-services-remove-subscription.mm
Last active August 29, 2015 14:05
ContextHub Event Services Remove Subscription Gist
// Unsubscribe to "office-chaione" tag and stop receiving events from beacon and geofence elements with that tag
NSString *tag = @"office-chaione";
if ([[CCHSensorPipeline sharedInstance] removeElementsWithTags:@[tag]]) {
NSLog(@"Successfully removed subscription to \"%@\" tag", tag);
} else {
NSLog(@"Failed to remove subscription to \"%@\" tag", tag);
}
// Remove a specific observer from your class (recommended)
[[NSNotificationCenter defaultCenter] removeObserver:self name:CCHSensorPipelineDidPostEvent object:nil];
@CHLibrarian
CHLibrarian / application-services-register-push.mm
Last active August 29, 2015 14:05
ContextHub Application Services Register Push Gist
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#ifdef DEBUG
// The debug flag is automatically set by the compiler, indicating which push gateway server your device will use
// Xcode deployed builds use the sandbox/development server
// TestFlight/App Store builds use the production server
// ContextHub records which environment a device is using so push works properly
// This must be called BEFORE [ContextHub registerWithAppId:]
[[ContextHub sharedInstance] setDebug:TRUE];
#endif
@CHLibrarian
CHLibrarian / application-services-setting-up-push.mm
Created August 17, 2014 18:25
ContextHub Application Services Setting Up Push Gist
// Set up a push dictionary
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
// Add a message (optional for background pushes if push has sound key)
userInfo[@"alert"] = @"alert";
// Add a sound (optional for background pushes if push has alert key)
userInfo[@"sound"] = @"default";
// Add custom data (optional)
@CHLibrarian
CHLibrarian / application-services-sending-to-device.mm
Created August 17, 2014 18:26
ContextHub Application Services Sending To Device Gist
// Send a push to 2 devices with IDs defined below
// Note: All device IDs are UUIDs, if a deviceID is not a valid UUID, then it is not a valid device ID
// Note: userDict is the push dictionary, see above on how to set it up
NSString *deviceID1 = @"20984403-690A-4098-8557-73B763F1DFFB";
NSString *deviceID2 = @"151E57AF-7E87-400F-A1E0-63C9E7811376";
[[CCHPush sharedInstance] sendNotificationToDevices:@[deviceID1, deviceID2] userInfo:userInfo completionHandler:^(NSError *error) {
if (!error) {
NSLog("Push to device ids \"%@, %@\" sent successfully", deviceID1, deviceID2);
} else {
@CHLibrarian
CHLibrarian / application-services-sending-to-alias.mm
Created August 17, 2014 18:28
ContextHub Application Services Sending To Alias Gist
// Send a push notification to 2 devices, with aliases "Michael's iPhone 5s" and "Jeff's iPhone 5"
// Note: userDict is the push dictionary, see above on how to set it up
NSString *alias1 = @"Michael's iPhone 5s";
NSString *alias2 = @"Jeff's iPhone 5";
[[CCHPush sharedInstance] sendNotificationToAliases:@[alias1, alias2] userInfo:userInfo completionHandler:^(NSError *error) {
if (!error) {
NSLog("Push to aliases \"%@, %@\" sent successfully", alias1, alias2);
} else {
NSLog("Push to aliases \"%@, %@\" failed", alias1, alias2);