diff --git a/src/ios/Plugins/AppDelegate+JPush.h b/src/ios/Plugins/AppDelegate+JPush.h index 3694db5..7c1f366 100644 --- a/src/ios/Plugins/AppDelegate+JPush.h +++ b/src/ios/Plugins/AppDelegate+JPush.h @@ -10,6 +10,4 @@ @interface AppDelegate (JPush) -//@property(nonatomic,strong)NSDictionary *luanchOption; - @end diff --git a/src/ios/Plugins/AppDelegate+JPush.m b/src/ios/Plugins/AppDelegate+JPush.m index 5c41cb4..542ea00 100644 --- a/src/ios/Plugins/AppDelegate+JPush.m +++ b/src/ios/Plugins/AppDelegate+JPush.m @@ -7,87 +7,42 @@ // #import "AppDelegate+JPush.h" -#import #import "JPushPlugin.h" #import "JPUSHService.h" -//static char launchNotificationKey; - @implementation AppDelegate (JPush) -+(void)load{ - - Method origin; - Method swizzle; - - origin=class_getInstanceMethod([self class],@selector(init)); - swizzle=class_getInstanceMethod([self class], @selector(init_plus)); - method_exchangeImplementations(origin, swizzle); +-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ + [JPushPlugin setLaunchOptions:launchOptions]; + return [super application:application didFinishLaunchingWithOptions:launchOptions]; } --(instancetype)init_plus{ - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(applicationDidLaunch:) - name:@"UIApplicationDidFinishLaunchingNotification" - object:nil]; - return [self init_plus]; -} - --(void)applicationDidLaunch:(NSNotification *)notification{ - - if (notification) { - [JPushPlugin setLaunchOptions:notification.userInfo]; - } -} - - - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; } + -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ - [JPUSHService handleRemoteNotification:userInfo]; - [[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification - object:userInfo]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification object:userInfo]; } -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ - [JPUSHService handleRemoteNotification:userInfo]; - [[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification - object:userInfo]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginReceiveNotification object:userInfo]; completionHandler(UIBackgroundFetchResultNewData); - } -- (void)application:(UIApplication *)application - didReceiveLocalNotification:(UILocalNotification *)notification { - [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil]; +- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { + [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil]; } - - (void)applicationWillEnterForeground:(UIApplication *)application { -// [application setApplicationIconBadgeNumber:0]; -// [application cancelAllLocalNotifications]; + // [application setApplicationIconBadgeNumber:0]; + // [application cancelAllLocalNotifications]; } - - (void)applicationDidEnterBackground:(UIApplication *)application { -// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; + // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } - - -//delegate里不能声明变量,所以采用关联对象这种技术绕过这个限制 -//-(NSDictionary *)luanchOption{ -// return objc_getAssociatedObject(self, &launchNotificationKey); -//} -//-(void)setLuanchOption:(NSDictionary *)luanchOption{ -// objc_setAssociatedObject(self, &launchNotificationKey, luanchOption, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -//} -//-(void)dealloc{ -// self.luanchOption=nil; -//} - @end diff --git a/src/ios/Plugins/JPushPlugin.h b/src/ios/Plugins/JPushPlugin.h index b86a217..dbc8d90 100644 --- a/src/ios/Plugins/JPushPlugin.h +++ b/src/ios/Plugins/JPushPlugin.h @@ -11,28 +11,50 @@ #define kJPushPluginReceiveNotification @"JPushPluginReceiveNofication" @interface JPushPlugin : CDVPlugin{ - + } +(void)setLaunchOptions:(NSDictionary *)theLaunchOptions; + +//以下为js中可调用接口 +//设置标签、别名 -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command; -(void)setTags:(CDVInvokedUrlCommand*)command; -(void)setAlias:(CDVInvokedUrlCommand*)command; + +//获取 RegistrationID -(void)getRegistrationID:(CDVInvokedUrlCommand*)command; + +//页面统计 -(void)startLogPageView:(CDVInvokedUrlCommand*)command; -(void)stopLogPageView:(CDVInvokedUrlCommand*)command; +-(void)beginLogPageView:(CDVInvokedUrlCommand*)command; -// 设置角标到服务器, 服务器下一次发消息时,会设置成这个值 +//设置角标到服务器,服务器下一次发消息时,会设置成这个值 //本接口不会改变应用本地的角标值. -(void)setBadge:(CDVInvokedUrlCommand*)command; //相当于 [setBadge:0] -(void)resetBadge:(CDVInvokedUrlCommand*)command; - -//改变应用本地的角标值. +//应用本地的角标值设置/获取 -(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command; -//获取应用本地的角标值. -(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command; +//停止与恢复推送 +-(void)stopPush:(CDVInvokedUrlCommand*)command; +-(void)resumePush:(CDVInvokedUrlCommand*)command; +-(void)isPushStopped:(CDVInvokedUrlCommand*)command; + +//开关日志 +-(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command; +-(void)setLogOFF:(CDVInvokedUrlCommand*)command; + +/* + * 以下为js中可监听到的事件 + * jpush.openNotification 点击推送消息唤醒或启动app + * jpush.setTagsWithAlias 设置标签、别名完成 + * jpush.receiveMessage 收到自定义消息 + * jpush.receiveNotification 前台收到推送消息 + */ @end diff --git a/src/ios/Plugins/JPushPlugin.m b/src/ios/Plugins/JPushPlugin.m index 1c37fc6..c8b1f63 100644 --- a/src/ios/Plugins/JPushPlugin.m +++ b/src/ios/Plugins/JPushPlugin.m @@ -10,452 +10,351 @@ #import "JPUSHService.h" #import - - static NSString *const JM_APP_KEY = @"APP_KEY"; static NSString *const JM_APP_CHANNEL = @"CHANNEL"; static NSString *const JM_APP_ISPRODUCTION = @"IsProduction"; - -static NSString *const JMessageConfigFileName = @"PushConfig"; - - -static NSDictionary *_luanchOptions=nil; +static NSString *const JPushConfigFileName = @"PushConfig"; +static NSDictionary *_luanchOptions = nil; @implementation JPushPlugin - -+(void)setLaunchOptions:(NSDictionary *)theLaunchOptions{ - _luanchOptions=theLaunchOptions; - [JPUSHService setDebugMode]; - if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { - //可以添加自定义categories - [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | - UIUserNotificationTypeSound | - UIUserNotificationTypeAlert) - categories:nil]; - } else { - //categories 必须为nil - [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | - UIRemoteNotificationTypeSound | - UIRemoteNotificationTypeAlert) - categories:nil]; - } - - //read appkey and channel from JMessageConfig.plist - NSString *plistPath = [[NSBundle mainBundle] pathForResource:JMessageConfigFileName ofType:@"plist"]; - if (plistPath == nil) { - NSLog(@"error: PushConfig.plist not found"); - assert(0); - } - - NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; - NSString * appkey = [plistData valueForKey:JM_APP_KEY]; - NSString * channel = [plistData valueForKey:JM_APP_CHANNEL]; - NSNumber * isProduction = [plistData valueForKey:JM_APP_ISPRODUCTION]; - - if (!appkey || appkey.length == 0) { - NSLog(@"error: app key not found in JMessageConfig.plist "); - assert(0); - } - - [JPUSHService setupWithOption:_luanchOptions appKey:appkey - channel:channel apsForProduction:[isProduction boolValue] ]; - -} - - +#pragma mark- 外部接口 -(void)stopPush:(CDVInvokedUrlCommand*)command{ - - [[UIApplication sharedApplication]unregisterForRemoteNotifications]; + [[UIApplication sharedApplication]unregisterForRemoteNotifications]; } - -(void)resumePush:(CDVInvokedUrlCommand*)command{ - -#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1 - if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { - //可以添加自定义categories - [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | - UIUserNotificationTypeSound | - UIUserNotificationTypeAlert) - categories:nil]; - } else { - //categories 必须为nil - [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | - UIRemoteNotificationTypeSound | - UIRemoteNotificationTypeAlert) - categories:nil]; - } -#else - //categories 必须为nil - [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | - UIRemoteNotificationTypeSound | - UIRemoteNotificationTypeAlert) - categories:nil]; -#endif - + [JPushPlugin registerForRemoteNotification]; } ++(void)registerForRemoteNotification{ + if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { + //可以添加自定义categories + [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | + UIUserNotificationTypeSound | + UIUserNotificationTypeAlert) + categories:nil]; + } else { +#ifndef __IPHONE_8_0 + //categories 必须为nil + [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | + UIRemoteNotificationTypeSound | + UIRemoteNotificationTypeAlert) + categories:nil]; +#endif + } +} -(void)isPushStopped:(CDVInvokedUrlCommand*)command{ - - NSNumber *result; - if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications ]) { - result=@(0); - }else{ - result=@(1); - } - CDVPluginResult * pushResult=[self pluginResultForValue:result]; - if (pushResult) { - [self succeedWithPluginResult:pushResult withCallbackID:command.callbackId]; - } else { - [self failWithCallbackID:command.callbackId]; - } + NSNumber *result; + if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) { + result = @(0); + }else{ + result = @(1); + } + [self hanleResultWithValue:result command:command]; } - -(void)initial:(CDVInvokedUrlCommand*)command{ - //do nithng,because Cordova plugin use lazy load mode. + //do nithng,because Cordova plugin use lazy load mode. } - #ifdef __CORDOVA_4_0_0 - (void)pluginInitialize { - NSLog(@"### pluginInitialize "); - [self initNotifications]; + NSLog(@"### pluginInitialize "); + [self initNotifications]; } #else - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView{ - NSLog(@"### initWithWebView "); - if (self=[super initWithWebView:theWebView]) { - [self initNotifications]; - - } - return self; + NSLog(@"### initWithWebView "); + if (self=[super initWithWebView:theWebView]) { + [self initNotifications]; + } + return self; } #endif - - -(void)initNotifications { - - NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; - [defaultCenter addObserver:self - selector:@selector(networkDidReceiveMessage:) - name:kJPFNetworkDidReceiveMessageNotification - object:nil]; - - [defaultCenter addObserver:self - selector:@selector(networkDidReceiveNotification:) - name:kJPushPluginReceiveNotification - object:nil]; - - if (_luanchOptions) { - NSDictionary *userInfo = [_luanchOptions - valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; - if ([userInfo count] >0) { - NSError *error; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; - NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; - if (!error) { - - dispatch_async(dispatch_get_main_queue(), ^{ - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]]; - }); - - } + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter addObserver:self + selector:@selector(networkDidReceiveMessage:) + name:kJPFNetworkDidReceiveMessageNotification + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(networkDidReceiveNotification:) + name:kJPushPluginReceiveNotification + object:nil]; + + if (_luanchOptions) { + NSDictionary *userInfo = [_luanchOptions + valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; + if ([userInfo count] >0) { + NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; + NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; + if (!error) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]]; + }); + + } + } + } - - } } - - -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{ - NSArray *arguments=command.arguments; - if (!arguments||[arguments count]<2) { - - NSLog(@"#### setTagsWithAlias param is less"); - return ; - } - NSString *alias=[arguments objectAtIndex:0]; - NSArray *arrayTags=[arguments objectAtIndex:1]; - - NSLog(@"#### setTagsWithAlias alias is %@, tags is %@",alias,arrayTags); - - NSSet* set=[NSSet setWithArray:arrayTags]; - [JPUSHService setTags:set - alias:alias - callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) - object:self]; + NSArray *arguments = command.arguments; + NSString *alias; + NSArray *tags; + if (!arguments || [arguments count] < 2) { + NSLog(@"#### setTagsWithAlias param is less"); + return ; + }else{ + alias = arguments[0]; + tags = arguments[1]; + } + + NSLog(@"#### setTagsWithAlias alias is %@, tags is %@",alias,tags); + + [JPUSHService setTags:[NSSet setWithArray:tags] + alias:alias + callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) + object:self]; } - - -(void)setTags:(CDVInvokedUrlCommand *)command{ - - - NSArray *arguments=[command arguments]; - NSString *tags=[arguments objectAtIndex:0]; - - NSLog(@"#### setTags %@",tags); - NSArray *array=[tags componentsSeparatedByString:@","]; - [JPUSHService setTags:[NSSet setWithArray:array] - callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) - object:self]; - + NSArray *tags = command.arguments; + + NSLog(@"#### setTags %@",tags); + + [JPUSHService setTags:[NSSet setWithArray:tags] + callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) + object:self]; + } - - -(void)setAlias:(CDVInvokedUrlCommand *)command{ - - NSArray *arguments=[command arguments]; - NSLog(@"#### setAlias %@",arguments); - [JPUSHService setAlias:[arguments objectAtIndex:0] - callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) - object:self]; - + + NSLog(@"#### setAlias %@",command.arguments); + [JPUSHService setAlias:command.arguments[0] + callbackSelector:@selector(tagsWithAliasCallback:tags:alias:) + object:self]; } - - -(void)getRegistrationID:(CDVInvokedUrlCommand*)command{ - NSString* registrationID = [JPUSHService registrationID]; - NSLog(@"### getRegistrationID %@",registrationID); - - CDVPluginResult *result=[self pluginResultForValue:registrationID]; - if (result) { - [self succeedWithPluginResult:result withCallbackID:command.callbackId]; - } else { - [self failWithCallbackID:command.callbackId]; - } + NSString* registrationID = [JPUSHService registrationID]; + NSLog(@"### getRegistrationID %@",registrationID); + [self hanleResultWithValue:registrationID command:command]; } - - - --(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{ - - NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithInt:resultCode],@"resultCode", - 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('jpush.setTagsWithAlias',%@)",jsonString]]; - // [self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.pushCallback('%@')",jsonString]]; - }); - -} - - -(void)startLogPageView:(CDVInvokedUrlCommand*)command{ - NSArray *arguments=command.arguments; - if (!arguments||[arguments count]<1) { - NSLog(@"startLogPageView argument error"); - return ; - } - NSString * pageName=[arguments objectAtIndex:0]; - if (pageName) { - [JPUSHService startLogPageView:pageName]; - } + NSArray *arguments = command.arguments; + if (!arguments || [arguments count] < 1) { + NSLog(@"startLogPageView argument error"); + return ; + } + NSString * pageName = arguments[0]; + if (pageName) { + [JPUSHService startLogPageView:pageName]; + } } - -(void)stopLogPageView:(CDVInvokedUrlCommand*)command{ - NSArray *arguments=command.arguments; - if (!arguments||[arguments count]<1) { - NSLog(@"stopLogPageView argument error"); - return ; - } - NSString * pageName=[arguments objectAtIndex:0]; - if (pageName) { - [JPUSHService stopLogPageView:pageName]; - } - + NSArray *arguments = command.arguments; + if (!arguments || [arguments count] < 1) { + NSLog(@"stopLogPageView argument error"); + return ; + } + NSString * pageName = arguments[0]; + if (pageName) { + [JPUSHService stopLogPageView:pageName]; + } + } - - -(void)beginLogPageView:(CDVInvokedUrlCommand*)command{ - NSArray *arguments=command.arguments; - if (!arguments||[arguments count]<2) { - NSLog(@"beginLogPageView argument error"); - return ; - } - NSString * pageName=[arguments objectAtIndex:0]; - int duration=[[arguments objectAtIndex:0]intValue]; - if (pageName) { - [JPUSHService beginLogPageView:pageName duration:duration]; - } - + NSArray *arguments = command.arguments; + if (!arguments || [arguments count] < 2) { + NSLog(@"beginLogPageView argument error"); + return ; + } + NSString * pageName = arguments[0]; + int duration = [arguments[0] intValue]; + if (pageName) { + [JPUSHService beginLogPageView:pageName duration:duration]; + } } - -(void)setBadge:(CDVInvokedUrlCommand*)command{ - NSArray *argument=command.arguments; - if ([argument count]<1) { - NSLog(@"setBadge argument error!"); - return; - } - NSNumber *badge=[argument objectAtIndex:0]; - [JPUSHService setBadge:[badge intValue]]; + NSArray *argument = command.arguments; + if ([argument count] < 1) { + NSLog(@"setBadge argument error!"); + return; + } + NSNumber *badge = argument[0]; + [JPUSHService setBadge:[badge intValue]]; } - - -(void)resetBadge:(CDVInvokedUrlCommand*)command{ - [JPUSHService resetBadge]; + [JPUSHService resetBadge]; } - -(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command{ - // - NSArray *argument=command.arguments; - if ([argument count]<1) { - NSLog(@"setBadge argument error!"); - return; - } - NSNumber *badge=[argument objectAtIndex:0]; - [UIApplication sharedApplication].applicationIconBadgeNumber=[badge intValue]; + // + NSArray *argument = command.arguments; + if ([argument count] < 1) { + NSLog(@"setBadge argument error!"); + return; + } + NSNumber *badge = [argument objectAtIndex:0]; + [UIApplication sharedApplication].applicationIconBadgeNumber = [badge intValue]; } - -(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command { - NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber; - CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:num]; - [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber; + NSNumber *number = [NSNumber numberWithInteger:num]; + [self hanleResultWithValue:number command:command]; } - - -(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command{ - - [JPUSHService setDebugMode]; + [JPUSHService setDebugMode]; } - - -(void)setLogOFF:(CDVInvokedUrlCommand*)command{ - - [JPUSHService setLogOFF]; + [JPUSHService setLogOFF]; } +#pragma mark- 内部方法 ++(void)setLaunchOptions:(NSDictionary *)theLaunchOptions{ + _luanchOptions = theLaunchOptions; + [JPUSHService setDebugMode]; -- (void)failWithCallbackID:(NSString *)callbackID { - CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; - [self.commandDelegate sendPluginResult:result callbackId:callbackID]; -} + [JPushPlugin registerForRemoteNotification]; - - -- (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]]; + //read appkey and channel from PushConfig.plist + NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfigFileName ofType:@"plist"]; + if (plistPath == nil) { + NSLog(@"error: PushConfig.plist not found"); + assert(0); } - } 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; + + NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; + NSString * appkey = [plistData valueForKey:JM_APP_KEY]; + NSString * channel = [plistData valueForKey:JM_APP_CHANNEL]; + NSNumber * isProduction = [plistData valueForKey:JM_APP_ISPRODUCTION]; + + if (!appkey || appkey.length == 0) { + NSLog(@"error: app key not found in PushConfig.plist "); + assert(0); + } + + [JPUSHService setupWithOption:_luanchOptions appKey:appkey + channel:channel apsForProduction:[isProduction boolValue] ]; } +#pragma mark 将参数返回给js +-(void)hanleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{ + CDVPluginResult *result = nil; + CDVCommandStatus status = CDVCommandStatus_OK; + + if ([value isKindOfClass:[NSString class]]) { + value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + } else if ([value isKindOfClass:[NSNull class]]) { + value = nil; + } + + if ([value isKindOfClass:[NSObject class]]) { + result = [CDVPluginResult resultWithStatus:status messageAsString:value];//NSObject 类型都可以 + } else { + NSLog(@"Cordova callback block returned unrecognized type: %@", NSStringFromClass([value class])); + result = nil; + } + + if (!result) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + } + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; +} + +#pragma mark 设置标签及别名回调 +-(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{ + + NSDictionary *dict = @{@"resultCode":[NSNumber numberWithInt:resultCode], + @"tags" :tags == nil ? [NSNull null] : [tags allObjects], + @"alias" :alias == nil ? [NSNull null] : 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('jpush.setTagsWithAlias',%@)",jsonString]]; + }); + +} - (void)networkDidReceiveMessage:(NSNotification *)notification { - - NSDictionary *userInfo = [notification userInfo]; - //NSLog(@"%@",userInfo); - - NSError *error; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; - NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; - - //NSLog(@"%@",jsonString); - - dispatch_async(dispatch_get_main_queue(), ^{ - - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.receiveMessage',%@)",jsonString]]; - - [self.commandDelegate evalJs:[NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveMessageIniOSCallback('%@')",jsonString]]; - - - }); - + if (notification) { + NSDictionary *userInfo = [notification userInfo]; + //NSLog(@"%@",userInfo); + + NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; + NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; + + //NSLog(@"%@",jsonString); + + dispatch_async(dispatch_get_main_queue(), ^{ + + [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.receiveMessage',%@)",jsonString]]; + + [self.commandDelegate evalJs:[NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveMessageIniOSCallback('%@')",jsonString]]; + + }); + } } - - -(void)networkDidReceiveNotification:(id)notification{ - - NSError *error; - NSDictionary *userInfo = [notification object]; - - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; - NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; - switch ([UIApplication sharedApplication].applicationState) { - case UIApplicationStateActive: - { - dispatch_async(dispatch_get_main_queue(), ^{ - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.receiveNotification',%@)",jsonString]]; - }); + + NSError *error; + NSDictionary *userInfo = [notification object]; + + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; + NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; + NSLog(@"%ld",(long)[UIApplication sharedApplication].applicationState); + switch ([UIApplication sharedApplication].applicationState) { + case UIApplicationStateActive: + { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.receiveNotification',%@)",jsonString]]; + }); + } + break; + case UIApplicationStateInactive: + case UIApplicationStateBackground: + { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]]; + }); + + } + break; + default: + //do nothing + break; } - break; - case UIApplicationStateInactive: - case UIApplicationStateBackground: - { - dispatch_async(dispatch_get_main_queue(), ^{ - [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]]; - }); - - } - break; - default: - //do nothing - break; - } - + } - @end