Created
March 19, 2011 23:42
-
-
Save tifroz/877914 to your computer and use it in GitHub Desktop.
APN processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PushNotification.h" | |
#import "JSON.h" | |
@implementation PushNotification | |
@synthesize notificationMessage; | |
@synthesize registerSuccessCallback; | |
@synthesize registerErrorCallback; | |
- (void)dealloc { | |
[notificationMessage release]; | |
[registerSuccessCallback release]; | |
[registerErrorCallback release]; | |
[super dealloc]; | |
} | |
- (void)registerAPN:(NSMutableArray *)arguments | |
withDict:(NSMutableDictionary *)options { | |
NSLog(@"registerAPN:%@\nwithDict:%@", arguments, options); | |
NSUInteger argc = [arguments count]; | |
if (argc > 0 && [[arguments objectAtIndex:0] length] > 0) | |
self.registerSuccessCallback = [arguments objectAtIndex:0]; | |
if (argc > 1 && [[arguments objectAtIndex:1] length] > 0) | |
self.registerErrorCallback = [arguments objectAtIndex:1]; | |
UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone; | |
if ([options objectForKey:@"badge"]) { | |
notificationTypes |= UIRemoteNotificationTypeBadge; | |
} | |
if ([options objectForKey:@"sound"]) { | |
notificationTypes |= UIRemoteNotificationTypeSound; | |
} | |
if ([options objectForKey:@"alert"]) { | |
notificationTypes |= UIRemoteNotificationTypeAlert; | |
} | |
if (notificationTypes == UIRemoteNotificationTypeNone) | |
NSLog(@"PushNotification.registerAPN: Push notification type is set to none"); | |
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; | |
NSLog(@"registerForRemoteNotificationTypes was called"); | |
} | |
- (void)startNotify:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options { | |
NSLog(@"startNotify:%@\nwithDict:%@", arguments, options); | |
ready = YES; | |
// Check if there is cached notification before JS PushNotification messageCallback is set | |
if (notificationMessage) { | |
[self notificationReceived]; | |
} | |
} | |
- (void)isEnabled:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options { | |
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; | |
NSString *jsStatement = [NSString stringWithFormat:@"navigator.pushNotification.isEnabled = %d;", type != UIRemoteNotificationTypeNone]; | |
[super writeJavascript:jsStatement]; | |
} | |
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken was called"); | |
NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] | |
stringByReplacingOccurrencesOfString:@">" withString:@""] | |
stringByReplacingOccurrencesOfString: @" " withString: @""]; | |
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken:%@", token); | |
if (registerSuccessCallback) { | |
NSString *jsStatement = [NSString stringWithFormat:@"%@({deviceToken:'%@'});", | |
registerSuccessCallback, token]; | |
[super writeJavascript:jsStatement]; | |
} | |
} | |
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { | |
NSLog(@"didFailToRegisterForRemoteNotificationsWithError was called"); | |
NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@", error); | |
if (registerErrorCallback) { | |
NSString *jsStatement = [NSString stringWithFormat:@"%@({error:'%@'});", | |
registerErrorCallback, error]; | |
[super writeJavascript:jsStatement]; | |
} | |
} | |
- (void)setNotificationMessage:(NSDictionary *)notification { | |
NSLog(@"setNotificationMessage:%@", notification); | |
[notification retain]; | |
[notificationMessage release]; | |
notificationMessage = notification; | |
if (notificationMessage) { | |
[self notificationReceived]; | |
} | |
} | |
- (void)notificationReceived { | |
NSLog(@"notificationReceived ready? %@", ready?@"YES":@"NO"); | |
if (ready) { | |
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init] ; | |
NSString *notificationJson = [jsonWriter stringWithObject:notificationMessage]; | |
NSLog(@"notificationJson: %@", notificationJson); | |
NSString *jsStatement = [NSString stringWithFormat:@"navigator.pushNotification.notificationCallback(%@);", notificationJson]; | |
NSLog(@"JS: %@", jsStatement); | |
[super writeJavascript:jsStatement]; | |
NSLog(@"run JS: %@", jsStatement); | |
self.notificationMessage = nil; | |
[jsonWriter release]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment