A Force.com wrapper for the Urban Airship Push API that supports device registration, single & batch push notifications, and storage of mobile device tokens.
The open source CaseMemo iPad app provides an example of its use, and is described in this video of Matthew Botos's Dreamforce '11 talk, Beyond the Force.com Toolkit for iOS. Other apps using the library include Mavens Consulting's Second Opinion.
A typical implementation for an iOS mobile app on Salesforce will have the following steps:
- In the iOS app, store the mobile device token when a user opts into push notifications:
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// NSData contains token as <abc1 defd ...> - strip to just alphanumerics
NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
token = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
ZKSObject *mobileDevice = [ZKSObject withType:@"Mobile_Device__c"];
[mobileDevice setFieldValue:token field:@"Name"];
// User__c will be set automatically by Salesforce trigger
[[FDCServerSwitchboard switchboard] create:[NSArray arrayWithObject:mobileDevice] target:self selector:@selector(createResult:error:context:) context:nil];
}
A trigger on Mobile Device will associate it with the current user.
-
In Salesforce, define a Push Notifications Settings custom object with the name of your iOS app and the API Key and Master Secret from Urban Airship.
-
When a push notification is to be sent from Force.com:
PushNotificationInterface pushNotificationService = new UrbanAirship('Case Memo');
pushNotificationService.sendPushNotification(deviceToken, message, badgeCount, userInfoJSON);
To avoid governor limits on web callouts, a batched queue of notifications can be sent:
pushNotificationService.queuePushNotification(deviceToken, message, badgeCount, userInfoJSON);
pushNotificationService.sendQueuedNotification();
- Additional test coverage
- Implement feedback API for inactive tokens
- Convert to use native Apex JSON functionality
To use the source code with a Salesforce org: How To Use Github and the Force.com IDE