Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
add basic push support methods
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Mar 6, 2013
1 parent 99d95b9 commit c657173
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
15 changes: 15 additions & 0 deletions UGAPI/UGClient.h
Expand Up @@ -238,6 +238,21 @@ set the response limit in UGQuery as well.
// is also handy for clients, so it is part of the interface.
+(NSString *)getUniqueDeviceID;

/***************** REMOTE PUSH NOTIFICATIONS *****************/

// call from application:didRegisterForRemoteNotificationsWithDeviceToken: callback
// will automatically register the passed deviceToken with the usergrid system
// using the getUniqueDeviceID method to associate this device on the server
- (UGClientResponse *)setDevicePushToken:(NSData *)newDeviceToken;

// push an "alert" type notification to the remote group, user, or device specified
// in the path argument. the notifer may be a name or UUID of an apns notifier
// that has been set up on the usergrid server.
- (UGClientResponse *)pushAlert:(NSString *)message
withSound:(NSString *)sound
to:(NSString *)path
usingNotifier:(NSString *)notifier;

/*********************** ACCESSORS ************************/
// if a user is logged in, this returns the OAuth token for this session.
// UGClient manages this internally, so you never really need it. But if you
Expand Down
57 changes: 57 additions & 0 deletions UGAPI/UGClient.m
Expand Up @@ -1086,6 +1086,63 @@ -(UGClientResponse *)removeSubscriber: (NSString *)queuePath subscriberPath:(NSS
return [self httpTransaction:url op:kUGHTTPDelete opData:nil];
}

/*************************** REMOTE PUSH NOTIFICATIONS ***************************/
/*************************** REMOTE PUSH NOTIFICATIONS ***************************/
/*************************** REMOTE PUSH NOTIFICATIONS ***************************/

- (UGClientResponse *)setDevicePushToken:(NSData *)newDeviceToken
{
// Pull the push token string out of the device token data
NSString *tokenString = [[[newDeviceToken description]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
stringByReplacingOccurrencesOfString:@" " withString:@""];

// Register device and push token to App Services
NSString *deviceId = [UGClient getUniqueDeviceID];

// create/update device - use deviceId for App Services entity UUID
NSMutableDictionary *entity = [[NSMutableDictionary alloc] init];
[entity setObject: @"device" forKey: @"type"];
[entity setObject: deviceId forKey: @"uuid"];
[entity setObject: tokenString forKey: @"apple.notifier.id"];

UGClientResponse *response = [self updateEntity: deviceId entity: entity];

if (response.transactionState == kUGClientResponseSuccess) {
// connect device to my user
response = [self connectEntities: @"users" connectorID: @"me" type: @"devices" connecteeID: deviceId];
}

return response;
}

- (UGClientResponse *)pushAlert:(NSString *)message
withSound:(NSString *)sound
to:(NSString *)path
usingNotifier:(NSString *)notifier
{
NSDictionary *apsDict = [NSDictionary dictionaryWithObjectsAndKeys:
message, @"alert",
sound, @"sound",
nil];

NSDictionary *notifierDict = [NSDictionary dictionaryWithObjectsAndKeys:
apsDict, @"aps",
nil];

NSDictionary *payloadsDict = [NSDictionary dictionaryWithObjectsAndKeys:
notifierDict, notifier,
nil];

NSString *notificationsPath = [path stringByAppendingString: @"/notifications"];

NSMutableDictionary *entity = [[NSMutableDictionary alloc] init];
[entity setObject: notificationsPath forKey: @"type"];
[entity setObject: payloadsDict forKey: @"payloads"];

return [self createEntity: entity];
}


/*************************** SERVER-SIDE STORAGE ***************************/
/*************************** SERVER-SIDE STORAGE ***************************/
Expand Down

0 comments on commit c657173

Please sign in to comment.