diff --git a/example/index.html b/example/index.html index 6a93060..dba7aeb 100644 --- a/example/index.html +++ b/example/index.html @@ -5,61 +5,21 @@ Phonegap Sample App - - - - @@ -145,10 +86,6 @@ -
- - -
@@ -183,7 +120,6 @@
- diff --git a/src/ios/Plugins/JPushPlugin.h b/src/ios/Plugins/JPushPlugin.h index 0abe0a9..8a323ea 100644 --- a/src/ios/Plugins/JPushPlugin.h +++ b/src/ios/Plugins/JPushPlugin.h @@ -16,7 +16,6 @@ -(void)setTags:(CDVInvokedUrlCommand*)command; -(void)setAlias:(CDVInvokedUrlCommand*)command; -(void)getRegistrationID:(CDVInvokedUrlCommand*)command; --(void)initNotifacationCenter:(CDVInvokedUrlCommand*)command; -(void)startLogPageView:(CDVInvokedUrlCommand*)command; -(void)stopLogPageView:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/Plugins/JPushPlugin.m b/src/ios/Plugins/JPushPlugin.m index 38b2e8c..3a5aeba 100644 --- a/src/ios/Plugins/JPushPlugin.m +++ b/src/ios/Plugins/JPushPlugin.m @@ -11,7 +11,6 @@ @implementation JPushPlugin - -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{ NSArray *arguments=command.arguments; @@ -21,18 +20,15 @@ } NSString *alias=[arguments objectAtIndex:0]; NSArray *arrayTags=[arguments objectAtIndex:1]; - // NSArray *tags=[arguments subarrayWithRange:range]; NSSet* set=[NSSet setWithArray:arrayTags]; [APService setTags:set alias:alias callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) object:self]; - //[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } -(void)setTags:(CDVInvokedUrlCommand *)command{ - //CDVPluginResult *pluginResult=nil; NSArray *arguments=[command arguments]; NSString *tags=[arguments objectAtIndex:0]; @@ -41,89 +37,52 @@ [APService setTags:[NSSet setWithArray:array] callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) object:self]; - //[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } -(void)setAlias:(CDVInvokedUrlCommand *)command{ - CDVPluginResult *pluginResult=nil; - NSArray *arguments=[command arguments]; [APService setAlias:[arguments objectAtIndex:0] callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) object:self]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } -(void)getRegistrationID:(CDVInvokedUrlCommand*)command{ + NSString* registratonID = [APService registrionID]; - dispatch_async(dispatch_get_main_queue(), ^{ - // - NSString *script=[NSString stringWithFormat:@"cordova.fireDocumentEvent('registrationID','[%@]')",registratonID]; - [self.commandDelegate evalJs:script]; - [self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.registrationCallback('%@')",registratonID]]; - }); - + CDVPluginResult *result=[self pluginResultForValue:registratonID]; + if (result) { + [self succeedWithPluginResult:result withCallbackID:command.callbackId]; + } else { + [self failWithCallbackID:command.callbackId]; + } } -(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{ - - NSLog(@"recode is %d tags is %@ alias %@",resultCode,tags,alias); NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:resultCode],@"resultCode", - tags==nil?[NSNull null]:[tags allObjects],@"resultTags", - alias==nil?[NSNull null]:alias,@"resultAlias",nil]; + tags==nil?[NSNull null]:[tags allObjects],@"tags", + alias==nil?[NSNull null]:alias,@"alias",nil]; + NSMutableDictionary *data = [NSMutableDictionary dictionary]; + [data setObject:[NSNumber numberWithInt:resultCode] forKey:@"resultCode"]; + [data setObject:tags==nil?[NSNull null]:[tags allObjects] forKey:@"tags"]; + [data setObject:alias==nil?[NSNull null]:alias forKey:@"alias"]; NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error]; NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; + dispatch_async(dispatch_get_main_queue(), ^{ - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('setTagsWithAlias','[%@]')",jsonString]]; - - [self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.pushCallback('%@')",jsonString]]; + [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.setTagsWithAlias',%@)",jsonString]]; + [self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.pushCallback('%@')",jsonString]]; }); -} -- (void)networkDidSetup:(NSNotification *)notification { - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidSetup')"]]; -} - -- (void)networkDidClose:(NSNotification *)notification { - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidClose')"]]; -} - -- (void)networkDidRegister:(NSNotification *)notification { - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidRegister')"]]; -} - -- (void)networkDidLogin:(NSNotification *)notification { - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidLogin')"]]; -} - --(void)initNotifacationCenter:(CDVInvokedUrlCommand*)command{ - - NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; - - [defaultCenter addObserver:self - selector:@selector(networkDidSetup:) - name:kAPNetworkDidSetupNotification - object:nil]; - [defaultCenter addObserver:self - selector:@selector(networkDidClose:) - name:kAPNetworkDidCloseNotification - object:nil]; - [defaultCenter addObserver:self - selector:@selector(networkDidRegister:) - name:kAPNetworkDidRegisterNotification - object:nil]; - [defaultCenter addObserver:self - selector:@selector(networkDidLogin:) - name:kAPNetworkDidLoginNotification - object:nil]; } + -(void)startLogPageView:(CDVInvokedUrlCommand*)command{ NSArray *arguments=command.arguments; if (!arguments||[arguments count]<1) { @@ -148,5 +107,39 @@ } +- (void)failWithCallbackID:(NSString *)callbackID { + CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + [self.commandDelegate sendPluginResult:result callbackId:callbackID]; +} +- (void)succeedWithPluginResult:(CDVPluginResult *)result withCallbackID:(NSString *)callbackID { + [self.commandDelegate sendPluginResult:result callbackId:callbackID]; +} +- (CDVPluginResult *)pluginResultForValue:(id)value { + + CDVPluginResult *result; + if ([value isKindOfClass:[NSString class]]) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK + messageAsString:[value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + } else if ([value isKindOfClass:[NSNumber class]]) { + CFNumberType numberType = CFNumberGetType((CFNumberRef)value); + //note: underlyingly, BOOL values are typedefed as char + if (numberType == kCFNumberIntType || numberType == kCFNumberCharType) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:[value intValue]]; + } else { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:[value doubleValue]]; + } + } else if ([value isKindOfClass:[NSArray class]]) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:value]; + } else if ([value isKindOfClass:[NSDictionary class]]) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:value]; + } else if ([value isKindOfClass:[NSNull class]]) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } else { + NSLog(@"Cordova callback block returned unrecognized type: %@", NSStringFromClass([value class])); + return nil; + } + return result; +} + @end diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index 31c7b07..14c2977 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -1,139 +1,88 @@ cordova.define("cn.jpush.phonegap.JPushPlugin.JPushPlugin", function(require, exports, module) { var JPushPlugin = function(){ - }; -JPushPlugin.prototype.call_native = function ( name, args) { - - ret = cordova.exec(null, - null, - 'JPushPlugin', - name, - args); - return ret; -} -JPushPlugin.prototype.getRegistrationID = function () { - - this.call_native( "getRegistrationID", null); +JPushPlugin.prototype.error_callback = function(msg){ + console.log("Javascript Callback Error: " + msg) } -JPushPlugin.prototype.startLogPageView = function (data) { - if (data==null || typeof(data)=="undefined" || data==""){ - console.log("argument is null"); +JPushPlugin.prototype.call_native = function(name, args, callback){ + ret = cordova.exec(callback,this.error_callback,'JPushPlugin',name,args); + return ret; +} + +JPushPlugin.prototype.getRegistrationID = function(callback){ + this.call_native("getRegistrationID",null,callback); +} + +JPushPlugin.prototype.startLogPageView = function(data){ + this.call_native( "startLogPageView",[data],null); +} + +JPushPlugin.prototype.stopLogPageView = function(data){ + this.call_native( "stopLogPageView",[data],null); +} + +JPushPlugin.prototype.setTagsWithAlias = function(tags,alias){ + if(tags==null){ + this.setAlias(alias); + return; } - else{ - this.call_native( "startLogPageView", [data]); - - } -} -JPushPlugin.prototype.stopLogPageView = function (data) { - if (data==null || typeof(data)=="undefined" || data==""){ - console.log("argument is null"); - - } - else{ - this.call_native( "stopLogPageView", [data]); - - } -} -JPushPlugin.prototype.initNotificationCenter = function () { - - this.call_native( "initNotifacationCenter", null); - -} -JPushPlugin.prototype.parseEvent = function (data) { - try{ - var parament="" - var count=1; - var start=false; - for(var i in data){ - - if(data[i]=='['){ - if (count==1&&start==false) { - start=true; - continue; - } - count++; - } - if (data[i]==']') { - if(count==1) - { - break; - } - count--; - } - - parament+=data[i] - - } - return parament; - } - catch(exception){ - alert(exception); - } + if(alias==null){ + this.setTags(tags); + return; + } + var arrayTagWithAlias=[tags]; + arrayTagWithAlias.unshift(alias); + this.call_native( "setTagsWithAlias", arrayTagWithAlias,null); } -JPushPlugin.prototype.setTagsWithAlias = function (tags,alias) { - - if(tags==null){ - this.setAlias(alias); - return; - } - if(alias==null){ - this.setTags(tags); - return; - } - var arrayTagWithAlias=[tags]; - arrayTagWithAlias.unshift(alias); - this.call_native( "setTagsWithAlias", arrayTagWithAlias); -} -JPushPlugin.prototype.setTags = function (data) { +JPushPlugin.prototype.setTags = function(data){ - try{ - this.call_native("setTags", [data]); - } - catch(exception){ - console.log(exception); - } + try{ + this.call_native("setTags",[data],null); + } + catch(exception){ + console.log(exception); + } } -JPushPlugin.prototype.setAlias = function (data) { - - try{ - - this.call_native("setAlias", [data]); - } - catch(exception){ - - console.log(exception); - } + +JPushPlugin.prototype.setAlias = function(data){ + try{ + this.call_native("setAlias", [data],null); + } + catch(exception){ + console.log(exception); + } } -JPushPlugin.prototype.pushCallback = function (data) { - try{ - var bToObj=JSON.parse(data); - var code = bToObj.resultCode; - var tags = bToObj.resultTags; - var alias = bToObj.resultAlias; - console.log("JPushPlugin:callBack--code is "+code+" tags is "+tags + " alias is "+alias); - } - catch(exception){ - - console.log(exception); - } + +JPushPlugin.prototype.pushCallback = function(data){ + try{ + var bToObj=JSON.parse(data); + var code = bToObj.resultCode; + var tags = bToObj.resultTags; + var alias = bToObj.resultAlias; + console.log("JPushPlugin:callBack--code is "+code+" tags is "+tags + " alias is "+alias); + } + catch(exception){ + console.log(exception); + } } -JPushPlugin.prototype.registrationCallback = function (data) { - try{ - console.log("registrationCallback--registraionID is "+data); - } - catch(exception){ - console.log(exception); - } +//android +//ios + +JPushPlugin.prototype.initNotificationCenter = function(){ + this.call_native( "initNotifacationCenter", null,null); } -if(!window.plugins) { - window.plugins = {}; + +if(!window.plugins){ + window.plugins = {}; } + if(!window.plugins.jPushPlugin){ - window.plugins.jPushPlugin = new JPushPlugin(); -} -module.exports = new JPushPlugin(); - + window.plugins.jPushPlugin = new JPushPlugin(); +} + +module.exports = new JPushPlugin(); + });