AppDelegate.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // AppDelegate.m
  3. // Apex And Drivers
  4. //
  5. // Created by Ray on 2018/5/26.
  6. // Copyright © 2018 USAI. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import <UserNotifications/UserNotifications.h>
  10. #import "LoginViewController.h"
  11. #import "RAHomeViewController.h"
  12. #import <CoreLocation/CoreLocation.h>
  13. #import "RANavigationController.h"
  14. #import "RAExceptionHandler.h"
  15. #import "RALoginNavigationController.h"
  16. #import "RAReachability.h"
  17. @interface AppDelegate ()<UNUserNotificationCenterDelegate,CLLocationManagerDelegate>
  18. @property (nonatomic,strong) CLLocationManager *locationManager;
  19. @property (nonatomic,strong) NSDictionary *currentOrderNotification;
  20. @end
  21. @implementation AppDelegate
  22. #pragma mark - Private
  23. - (void)showHomeVC{
  24. RAHomeViewController *homeVC = [RAHomeViewController viewControllerFromStoryboard];
  25. RANavigationController *nav = [[RANavigationController alloc] initWithRootViewController:homeVC];
  26. self.window.rootViewController = nav;
  27. }
  28. - (void)showLoginVC {
  29. LoginViewController *rootVC = [LoginViewController viewControllerFromStoryboard];
  30. __weak typeof(self) weakSelf = self;
  31. rootVC.loginSuccessful = ^(NSString *user,NSString *password, NSString *firstName){
  32. [[RASingleton sharedInstance] loginUser:user password:password firstName:firstName];
  33. [weakSelf showHomeVC];
  34. };
  35. RALoginNavigationController *nav = [[RALoginNavigationController alloc] initWithRootViewController:rootVC];
  36. self.window.rootViewController = nav;
  37. }
  38. - (void)saveUploadTasks {
  39. if(self.uploadManager.arr_queue!=nil)
  40. {
  41. [self.uploadManager stopAllTasks];
  42. [self.uploadManager saveTasks];
  43. }
  44. }
  45. //- (void)requestLocation
  46. //{
  47. // [self initLocationManager];
  48. // [self.locationManager requestLocation];
  49. //}
  50. -(void)initLocationManager
  51. {
  52. if (self.locationManager) {
  53. return;
  54. }
  55. self.locationManager = [[CLLocationManager alloc] init];
  56. self.locationManager.delegate = self;
  57. self.locationManager.allowsBackgroundLocationUpdates = NO;
  58. self.locationManager.pausesLocationUpdatesAutomatically = NO; // 是否允许系统自动暂停定位
  59. self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;//设置定位精度
  60. self.locationManager.distanceFilter = 10; //不需要移动都可以刷新,不然不移动就不会执行定位,不定位的话,那么后台进程也就挂起了
  61. [self.locationManager requestAlwaysAuthorization];
  62. }
  63. - (void)startBackgroundLocation {
  64. [self.locationManager startUpdatingLocation];
  65. self.locationManager.allowsBackgroundLocationUpdates=false;
  66. }
  67. - (void)stopBackgroundLocation {
  68. if (self.locationManager == nil) {
  69. return;
  70. }
  71. // [self.locationManager stopUpdatingLocation];
  72. self.locationManager.allowsBackgroundLocationUpdates=false;
  73. // self.locationManager = nil;
  74. }
  75. - (void)sendUploadCountNotification {
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. NSUInteger count = self.uploadManager.arr_queue.count;
  78. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationChangeUpload object:nil userInfo:@{@"upload_count" : @(count)}];
  79. });
  80. }
  81. - (void)receiveLogoutNotification:(NSNotification *)notification {
  82. [self showLoginVC];
  83. }
  84. - (void)receiveStartBackgroundLocationNotification:(NSNotification *)notification {
  85. [self startBackgroundLocation];
  86. }
  87. - (void)receiveStopBackgroundLocationNotification:(NSNotification *)notification {
  88. [self stopBackgroundLocation];
  89. }
  90. - (void)receiveUploadFinishNotification:(NSNotification *)notification {
  91. [self sendUploadCountNotification];
  92. }
  93. - (void)receiveUploadCountChangeNotification:(NSNotification *)notification {
  94. [self sendUploadCountNotification];
  95. }
  96. - (void)receiveRequestUploadCountNotification:(NSNotification *)notification {
  97. [self sendUploadCountNotification];
  98. }
  99. - (void)registActionNotification {
  100. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLogoutNotification:) name:RANotificationLogout object:nil];
  101. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveStartBackgroundLocationNotification:) name:RANotificationStartBackgroundLocation object:nil];
  102. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveStopBackgroundLocationNotification:) name:RANotificationStopBackgroundLocation object:nil];
  103. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveUploadFinishNotification:) name:UPLOAD_FINISH_NOTIFICATION object:nil];
  104. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveUploadFinishNotification:) name:UPLOAD_COUNT_CHANGE_NOTIFICATION object:nil];
  105. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveRequestUploadCountNotification:) name:RANotificationRequestUploadCount object:nil];
  106. }
  107. #pragma mark - AppDelegate
  108. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  109. [self initLocationManager];
  110. // Override point for customization after application launch.
  111. // NSString *orderID = nil;
  112. // NSInteger orderType = OrderTypeNew;
  113. // NSString *orderType2 = nil;
  114. // if (launchOptions) {
  115. // NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  116. // NSDictionary *aps = [userInfo objectForKey:@"aps"];
  117. // orderID = [aps objectForKey:@"order-id"];
  118. // orderType = [[aps objectForKey:@"order-type"] integerValue];
  119. // orderType2 = [aps objectForKey:@"order-type2"];
  120. //
  121. // }
  122. [RAExceptionHandler registHandler:^(NSString *exceptionStr) {
  123. NSLog(@"Exception:\n%@",exceptionStr);
  124. }];
  125. // 禁止Disk缓存,否则会和程序实现的缓存重复
  126. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  127. #ifdef OFFLINE_MODE
  128. [[RAReachability defaultReachability] startNotifier:^(BOOL reachable) {
  129. [RASingleton sharedInstance].offline = !reachable;
  130. }];
  131. #endif
  132. // [RADataProvider downloadOfflineData];
  133. // [RASingleton sharedInstance].offline = YES;
  134. // View
  135. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  136. self.window.backgroundColor = [UIColor whiteColor];
  137. if (RASingleton.sharedInstance.autoLogin) {
  138. [self showHomeVC];
  139. } else {
  140. [self showLoginVC];
  141. }
  142. [self.window makeKeyAndVisible];
  143. [RASingleton.sharedInstance loadSavedReuqiredLocation];
  144. //
  145. [self registActionNotification];
  146. //消息推送注册
  147. UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
  148. [center setDelegate:self];
  149. UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
  150. [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
  151. if (granted) {
  152. NSLog(@"推送通知授权成功");
  153. }else{
  154. NSLog(@"推送通知授权失败");
  155. }
  156. }];
  157. [application registerForRemoteNotifications];
  158. // 程序被强杀之后收到通知,点击 通知 启动应用
  159. if (launchOptions && application.applicationState == UIApplicationStateInactive) {
  160. NSMutableDictionary *userInfo = [[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] mutableCopy];
  161. if (userInfo) {
  162. [userInfo setObject:@(YES) forKey:@"go2Detail"];
  163. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  164. }
  165. }
  166. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s launchOptions:%@",application.applicationState,__func__,launchOptions]];
  167. return YES;
  168. }
  169. - (void)applicationDidBecomeActive:(UIApplication *)application {
  170. if (!self.uploadManager) {
  171. [RAUploadManager configureUploadManager:^(RAUPloadManagerConfigure *configure) {
  172. configure.autoRemoveFinish = YES;
  173. // configure.retryCount = 1;
  174. // configure.retryTimeIntetval = 10;
  175. }];
  176. self.uploadManager = [RAUploadManager sharedManager];
  177. }
  178. // [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  179. // 程序在后台收到通知后,直接打开程序的情况下弹窗提示
  180. if (self.currentOrderNotification) {
  181. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:self.currentOrderNotification];
  182. self.currentOrderNotification = nil;
  183. }
  184. }
  185. - (void)applicationWillTerminate:(UIApplication *)application {
  186. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  187. [self saveUploadTasks];
  188. }
  189. - (void)dealloc {
  190. [[NSNotificationCenter defaultCenter] removeObserver:self];
  191. }
  192. #pragma mark - Notification
  193. //完成注册后收到devicetoken
  194. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  195. //上传deviceToken给后台服务器
  196. if (![deviceToken isKindOfClass:[NSData class]]) return;
  197. const unsigned *tokenBytes = [deviceToken bytes];
  198. NSString *deviceTokenStr = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
  199. ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
  200. ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
  201. ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
  202. NSLog(@"deviceToken:%@",deviceTokenStr);
  203. // NSString *fullStr = [deviceToken description];
  204. // NSLog(@"fullstr= %@",fullStr);
  205. // NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
  206. // NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
  207. [[RASingleton sharedInstance] setNotificationToken:deviceTokenStr];
  208. }
  209. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  210. NSLog(@"注册推送失败:%@",error);
  211. }
  212. //处理通知
  213. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  214. // 程序在前台收到通知
  215. UNNotificationRequest *request = notification.request;
  216. NSDictionary *userInfo = request.content.userInfo;
  217. if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  218. NSLog(@"收到远程通知: %@",userInfo);
  219. } else {
  220. NSLog(@"收到本地通知: %@",userInfo);
  221. }
  222. // UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert
  223. completionHandler(UNNotificationPresentationOptionNone);
  224. }
  225. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  226. NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
  227. // 点击通知
  228. // NSDictionary *userInfo = response.notification.request.content.userInfo;
  229. // if (userInfo) {
  230. //
  231. // NSDictionary *aps = [userInfo objectForKey:@"aps"];
  232. //
  233. // NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue];
  234. // if (is_order) {
  235. // [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  236. // }
  237. // }
  238. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",[UIApplication sharedApplication].applicationState,__func__]];
  239. self.currentOrderNotification = nil;
  240. NSMutableDictionary *userInfo = [response.notification.request.content.userInfo mutableCopy];
  241. [userInfo setObject:@(YES) forKey:@"go2Detail"];
  242. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  243. completionHandler();
  244. }
  245. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  246. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",application.applicationState,__func__]];
  247. /**
  248. 静默推送alert为空
  249. {
  250. "aps" : {
  251. "alert" : "",
  252. "content-available" : 1
  253. }
  254. }
  255. */
  256. // "content-available" : 1 收到通知
  257. NSLog(@"收到静默推送: %@",userInfo);
  258. if (userInfo) {
  259. NSDictionary *aps = [userInfo objectForKey:@"aps"];
  260. // [UIApplication sharedApplication].applicationIconBadgeNumber = [aps[@"badge"] intValue];
  261. NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue];
  262. if (report_location == 1) {
  263. NSString *orderID = [aps objectForKey:@"order-id"];
  264. BOOL tracing = [[aps objectForKey:@"tracing"] boolValue];
  265. NSString *locationId = [aps objectForKey:@"location-id"];
  266. if (tracing) {
  267. if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  268. [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  269. } else {
  270. [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject tracing by app setting Driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  271. }
  272. return;
  273. }
  274. if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  275. [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  276. } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlwaysAsk) {
  277. [self askForReportLastLocation:orderID locationID:locationId];
  278. } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeReject) {
  279. [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject ping by app setting,driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  280. }
  281. completionHandler(UIBackgroundFetchResultNewData);
  282. } else {
  283. /**
  284. {
  285. "aps" : {
  286. "alert" : {
  287. "title" : "Notification",
  288. "body" : "You have a new order"
  289. },
  290. "is-order" : 1,
  291. "order-id" : "AFS20180530001200",
  292. "order-type2" : "Pick Up",
  293. "order-type" : 2,
  294. "sound" : "default",
  295. "content-available" : 1
  296. }
  297. }
  298. */
  299. NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue];
  300. if (is_order) {
  301. // 程序在前台,弹窗提示
  302. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
  303. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  304. } else {
  305. // 程序在后台,记录下来。当程序重新进入前台时弹窗提示(没有点击通知打开程序的情况)
  306. self.currentOrderNotification = userInfo;
  307. }
  308. }
  309. completionHandler(UIBackgroundFetchResultNewData);
  310. }
  311. } else {
  312. completionHandler(UIBackgroundFetchResultNoData);
  313. }
  314. }
  315. #pragma mark - Local Notification
  316. - (void)sendLocalNotification:(NSString *)title message:(NSString *)msg {
  317. // 创建Content
  318. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  319. content.title = title;
  320. content.body = msg;
  321. content.badge = @([UIApplication sharedApplication].applicationIconBadgeNumber + 1);
  322. // 创建Request,保证ID一致
  323. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"local_notification" content:content trigger:nil];
  324. // 发送
  325. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  326. if (!error) {
  327. NSLog(@"发送本地通知:%@",request.identifier);
  328. }
  329. }];
  330. }
  331. #pragma mark - Report Location
  332. - (void)rejectReportLocationWithReason:(NSString *)reason forOrder:(NSString *)orderID locationID:(NSString *)locationId {
  333. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  334. [RADataProvider reportLocationWithUserReason:reason forOrder:orderID locationID:locationId];
  335. });
  336. }
  337. - (void)reportLastLocation:(CLLocation *)location forOrder:(NSString *)orderID locationID:(NSString *)locationId {
  338. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  339. [RADataProvider reportLastLocation:location forOrderID:orderID locationID:locationId];
  340. });
  341. }
  342. - (void)askForReportLastLocation:(NSString *)orderID locationID:(NSString *)locationId{
  343. UIViewController *topVC = self.window.rootViewController;
  344. while (topVC.presentedViewController) {
  345. topVC = topVC.presentedViewController;
  346. }
  347. if (topVC) {
  348. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Report Location For Order:%@",orderID] preferredStyle:UIAlertControllerStyleAlert];
  349. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  350. [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Driver %@ choose cancel of report location",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  351. }];
  352. __weak typeof(self) weakSelf = self;
  353. UIAlertAction *reportAction = [UIAlertAction actionWithTitle:@"Report" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  354. [weakSelf reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  355. }];
  356. [alertVC addAction:cancelAction];
  357. [alertVC addAction:reportAction];
  358. [topVC presentViewController:alertVC animated:YES completion:nil];
  359. [self sendLocalNotification:@"Report Location Notification" message:[NSString stringWithFormat:@"The Apex ask your location for order:%@",orderID]];
  360. }
  361. }
  362. #pragma mark - LocationManager Delegate
  363. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  364. if (locations.count) {
  365. [RASingleton sharedInstance].currentLocation = [locations lastObject];
  366. [RASingleton sharedInstance].lastLocation = [locations lastObject];
  367. [RASingleton sharedInstance].lastLocationDateTime=[RAUtils current_date];
  368. NSLog(@"location: %f, %f",[RASingleton sharedInstance].currentLocation.coordinate.latitude,[RASingleton sharedInstance].currentLocation.coordinate.longitude);
  369. // 省电,停止不能超过三分钟
  370. // [self performSelector:@selector(stopLocation) withObject:nil afterDelay:10]; // 获取到位置10s后关闭位置服务
  371. // [self performSelector:@selector(startLocation) withObject:nil afterDelay:120]; // 获取到位置120s后重新打开位置服务
  372. //后台延迟定位3km,5分钟。
  373. CLLocationDistance distance = 500;
  374. NSTimeInterval time = 60;
  375. if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
  376. {
  377. // more aggressive when app is active
  378. distance = 20;
  379. time = 20;
  380. }
  381. // if(!manager.allowsBackgroundLocationUpdates)
  382. // {
  383. //
  384. // }
  385. // [manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
  386. }
  387. }
  388. - (void)locationManager:(CLLocationManager *)manager
  389. didFailWithError:(NSError *)error
  390. {
  391. [RASingleton sharedInstance].currentLocation = nil;
  392. }
  393. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  394. if (status == kCLAuthorizationStatusDenied) {
  395. [RASingleton sharedInstance].currentLocation = nil;
  396. }else
  397. {
  398. [self.locationManager startUpdatingLocation];
  399. }
  400. }
  401. @end