JPushPlugin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // PushTalkPlugin.m
  3. // PushTalk
  4. //
  5. // Created by zhangqinghe on 13-12-13.
  6. //
  7. //
  8. #import "JPushPlugin.h"
  9. #import "JPUSHService.h"
  10. #import <UIKit/UIKit.h>
  11. #import <AdSupport/AdSupport.h>
  12. #import <UserNotifications/UserNotifications.h>
  13. #import "AppDelegate+JPush.h"
  14. #import "JPushDefine.h"
  15. @implementation NSDictionary (JPush)
  16. -(NSString*)toJsonString{
  17. NSError *error;
  18. NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
  19. NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  20. return jsonString;
  21. }
  22. @end
  23. @implementation NSString (JPush)
  24. -(NSDictionary*)toDictionary{
  25. NSError *error;
  26. NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
  27. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  28. return dict;
  29. }
  30. @end
  31. @interface JPushPlugin()
  32. @end
  33. @implementation JPushPlugin
  34. -(void)startJPushSDK:(CDVInvokedUrlCommand*)command{
  35. [(AppDelegate*)[UIApplication sharedApplication].delegate startJPushSDK];
  36. }
  37. #pragma mark- 外部接口
  38. -(void)stopPush:(CDVInvokedUrlCommand*)command{
  39. [[UIApplication sharedApplication]unregisterForRemoteNotifications];
  40. }
  41. -(void)resumePush:(CDVInvokedUrlCommand*)command{
  42. [(AppDelegate*)[UIApplication sharedApplication].delegate registerForRemoteNotification];
  43. }
  44. -(void)isPushStopped:(CDVInvokedUrlCommand*)command{
  45. NSNumber *result = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications] ? @(0) : @(1);
  46. [self handleResultWithValue:result command:command];
  47. }
  48. -(void)initial:(CDVInvokedUrlCommand*)command{
  49. //do nithng,because Cordova plugin use lazy load mode.
  50. }
  51. #ifdef __CORDOVA_4_0_0
  52. - (void)pluginInitialize {
  53. NSLog(@"### pluginInitialize ");
  54. [self initPlugin];
  55. }
  56. #else
  57. - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView{
  58. NSLog(@"### initWithWebView ");
  59. if (self=[super initWithWebView:theWebView]) {
  60. }
  61. [self initPlugin];
  62. return self;
  63. }
  64. #endif
  65. -(void)initPlugin{
  66. if (!SharedJPushPlugin) {
  67. SharedJPushPlugin = self;
  68. }
  69. [[NSNotificationCenter defaultCenter] addObserver:self
  70. selector:@selector(networkDidReceiveMessage:)
  71. name:kJPFNetworkDidReceiveMessageNotification
  72. object:nil];
  73. }
  74. +(void)fireDocumentEvent:(NSString*)eventName jsString:(NSString*)jsString{
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. [SharedJPushPlugin.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.%@',%@)", eventName, jsString]];
  77. });
  78. }
  79. -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{
  80. NSString *alias = [command argumentAtIndex:0];
  81. NSArray *tags = [command argumentAtIndex:1];
  82. [JPUSHService setTags:[NSSet setWithArray:tags]
  83. alias:alias
  84. fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
  85. CDVPluginResult *result;
  86. if (iResCode == 0) {
  87. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  88. } else {
  89. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:nil];
  90. }
  91. [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
  92. }];
  93. }
  94. -(void)setTags:(CDVInvokedUrlCommand*)command{
  95. NSArray *tags = command.arguments;
  96. [JPUSHService setTags:[NSSet setWithArray:tags]
  97. alias:nil
  98. fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
  99. CDVPluginResult *result;
  100. if (iResCode == 0) {
  101. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  102. } else {
  103. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:nil];
  104. }
  105. [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
  106. }];
  107. }
  108. -(void)setAlias:(CDVInvokedUrlCommand*)command{
  109. NSString *alias = [command argumentAtIndex:0];
  110. [JPUSHService setTags:nil
  111. alias:alias
  112. fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
  113. CDVPluginResult *result;
  114. if (iResCode == 0) {
  115. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  116. } else {
  117. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:nil];
  118. }
  119. [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
  120. }];
  121. }
  122. -(void)getRegistrationID:(CDVInvokedUrlCommand*)command{
  123. NSString* registrationID = [JPUSHService registrationID];
  124. [self handleResultWithValue:registrationID command:command];
  125. }
  126. -(void)startLogPageView:(CDVInvokedUrlCommand*)command{
  127. NSString * pageName = [command argumentAtIndex:0];
  128. [JPUSHService startLogPageView:pageName];
  129. }
  130. -(void)stopLogPageView:(CDVInvokedUrlCommand*)command{
  131. NSString * pageName = [command argumentAtIndex:0];
  132. [JPUSHService stopLogPageView:pageName];
  133. }
  134. -(void)beginLogPageView:(CDVInvokedUrlCommand*)command{
  135. NSString *pageName = [command argumentAtIndex:0];
  136. NSNumber *duration = [command argumentAtIndex:1];
  137. [JPUSHService beginLogPageView:pageName duration:duration.intValue];
  138. }
  139. -(void)setBadge:(CDVInvokedUrlCommand*)command{
  140. NSNumber *badge = [command argumentAtIndex:0];
  141. [JPUSHService setBadge:badge.intValue];
  142. }
  143. -(void)resetBadge:(CDVInvokedUrlCommand*)command{
  144. [JPUSHService resetBadge];
  145. }
  146. -(void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command{
  147. NSNumber *badge = [command argumentAtIndex:0];
  148. [UIApplication sharedApplication].applicationIconBadgeNumber = badge.intValue;
  149. }
  150. -(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command {
  151. NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber;
  152. NSNumber *number = [NSNumber numberWithInteger:num];
  153. [self handleResultWithValue:number command:command];
  154. }
  155. -(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command{
  156. [JPUSHService setDebugMode];
  157. }
  158. -(void)setLogOFF:(CDVInvokedUrlCommand*)command{
  159. [JPUSHService setLogOFF];
  160. }
  161. -(void)crashLogON:(CDVInvokedUrlCommand*)command{
  162. [JPUSHService crashLogON];
  163. }
  164. -(void)setLocalNotification:(CDVInvokedUrlCommand*)command{
  165. NSLog(@"ios 10 after please use UNNotificationRequest to set local notification, see apple doc to learn more");
  166. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:[[command argumentAtIndex:0] intValue]];
  167. NSString *alert = [command argumentAtIndex:1];
  168. NSNumber *badge = [command argumentAtIndex:2];
  169. NSString *idKey = [command argumentAtIndex:3];
  170. NSDictionary *dict = [command argumentAtIndex:4];
  171. [JPUSHService setLocalNotification:date alertBody:alert badge:badge.intValue alertAction:nil identifierKey:idKey userInfo:dict soundName:nil];
  172. }
  173. -(void)deleteLocalNotificationWithIdentifierKey:(CDVInvokedUrlCommand*)command{
  174. NSString *identifier = [command argumentAtIndex:0];
  175. JPushNotificationIdentifier *jpid = [JPushNotificationIdentifier new];
  176. jpid.identifiers = @[identifier];
  177. [JPUSHService removeNotification:jpid];
  178. }
  179. -(void)clearAllLocalNotifications:(CDVInvokedUrlCommand*)command{
  180. [JPUSHService removeNotification:nil];
  181. }
  182. -(void)setLocation:(CDVInvokedUrlCommand*)command{
  183. NSNumber *latitude = [command argumentAtIndex:0];
  184. NSNumber *longitude = [command argumentAtIndex:1];
  185. [JPUSHService setLatitude:latitude.doubleValue longitude:longitude.doubleValue];
  186. }
  187. -(void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command{
  188. if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
  189. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  190. WEAK_SELF(weakSelf);
  191. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  192. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  193. dict[@"authorizationStatus"] = @(settings.authorizationStatus);
  194. dict[@"soundSetting"] = @(settings.soundSetting);
  195. dict[@"badgeSetting"] = @(settings.badgeSetting);
  196. dict[@"alertSetting"] = @(settings.alertSetting);
  197. dict[@"notificationCenterSetting"] = @(settings.notificationCenterSetting);
  198. dict[@"lockScreenSetting"] = @(settings.lockScreenSetting);
  199. dict[@"carPlaySetting"] = @(settings.carPlaySetting);
  200. dict[@"alertStyle"] = @(settings.alertStyle);
  201. [weakSelf handleResultWithValue:dict command:command];
  202. }];
  203. }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  204. UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
  205. UIUserNotificationType type = settings.types;
  206. NSNumber *number = [NSNumber numberWithInteger:type];
  207. [self handleResultWithValue:number command:command];
  208. }else{
  209. UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
  210. NSNumber *number = [NSNumber numberWithInteger:type];
  211. [self handleResultWithValue:number command:command];
  212. }
  213. }
  214. #pragma mark - ios 10 APIs
  215. -(void)addDismissActions:(CDVInvokedUrlCommand*)command{
  216. [self addActions:command dismiss:YES];
  217. }
  218. -(void)addNotificationActions:(CDVInvokedUrlCommand*)command{
  219. [self addActions:command dismiss:NO];
  220. }
  221. -(void)addActions:(CDVInvokedUrlCommand*)command dismiss:(BOOL)dimiss{
  222. NSArray *actionsData = [command argumentAtIndex:0];
  223. NSString *categoryId = [command argumentAtIndex:1];
  224. NSMutableArray *actions = [NSMutableArray array];
  225. for (NSDictionary *dict in actionsData) {
  226. NSString *title = dict[@"title"];
  227. NSString *identifier = dict[@"identifier"];
  228. NSString *option = dict[@"option"];
  229. NSString *type = dict[@"type"];
  230. if ([type isEqualToString:@"textInput"]) {
  231. NSString *textInputButtonTitle = dict[@"textInputButtonTitle"];
  232. NSString *textInputPlaceholder = dict[@"textInputPlaceholder"];
  233. UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:identifier title:title options:option.integerValue textInputButtonTitle:textInputButtonTitle textInputPlaceholder:textInputPlaceholder];
  234. [actions addObject:inputAction];
  235. } else {
  236. UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:title title:title options:option.integerValue];
  237. [actions addObject:action];
  238. }
  239. }
  240. UNNotificationCategory *category;
  241. if (dimiss) {
  242. category = [UNNotificationCategory categoryWithIdentifier:categoryId
  243. actions:actions
  244. intentIdentifiers:@[]
  245. options:UNNotificationCategoryOptionCustomDismissAction];
  246. } else {
  247. category = [UNNotificationCategory categoryWithIdentifier:categoryId
  248. actions:actions
  249. intentIdentifiers:@[]
  250. options:UNNotificationCategoryOptionNone];
  251. }
  252. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
  253. }
  254. #pragma mark - 内部方法
  255. +(void)setupJPushSDK:(NSDictionary*)userInfo{
  256. NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfig_FileName ofType:@"plist"];
  257. if (plistPath == nil) {
  258. NSLog(@"error: PushConfig.plist not found");
  259. assert(0);
  260. }
  261. NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  262. NSString *appkey = [plistData valueForKey:JPushConfig_Appkey];
  263. NSString *channel = [plistData valueForKey:JPushConfig_Channel];
  264. NSNumber *isProduction = [plistData valueForKey:JPushConfig_IsProduction];
  265. NSNumber *isIDFA = [plistData valueForKey:JPushConfig_IsIDFA];
  266. NSString *advertisingId = nil;
  267. if(isIDFA.boolValue) {
  268. advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  269. }
  270. [JPUSHService setupWithOption:userInfo
  271. appKey:appkey
  272. channel:channel
  273. apsForProduction:[isProduction boolValue]
  274. advertisingIdentifier:advertisingId];
  275. }
  276. #pragma mark 将参数返回给js
  277. -(void)handleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command {
  278. CDVPluginResult *result = nil;
  279. CDVCommandStatus status = CDVCommandStatus_OK;
  280. if ([value isKindOfClass:[NSString class]]) {
  281. value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  282. } else if ([value isKindOfClass:[NSNull class]]) {
  283. value = nil;
  284. }
  285. if ([value isKindOfClass:[NSObject class]]) {
  286. result = [CDVPluginResult resultWithStatus:status messageAsString:value];//NSObject 类型都可以
  287. } else {
  288. NSLog(@"Cordova callback block returned unrecognized type: %@", NSStringFromClass([value class]));
  289. result = nil;
  290. }
  291. if (!result) {
  292. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
  293. }
  294. [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
  295. }
  296. #pragma mark 设置标签及别名回调
  297. -(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias {
  298. if (resultCode == 0) { // Success
  299. } else {
  300. }
  301. }
  302. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  303. if (notification && notification.userInfo) {
  304. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveMessage
  305. jsString:[notification.userInfo toJsonString]];
  306. }
  307. }
  308. @end