AppDelegate.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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=true;
  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. NSString *fullStr = [deviceToken description];
  197. NSLog(@"fullstr= %@",fullStr);
  198. NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
  199. NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
  200. [[RASingleton sharedInstance] setNotificationToken:deviceTokenStr];
  201. }
  202. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  203. NSLog(@"注册推送失败:%@",error);
  204. }
  205. //处理通知
  206. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  207. // 程序在前台收到通知
  208. UNNotificationRequest *request = notification.request;
  209. NSDictionary *userInfo = request.content.userInfo;
  210. if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  211. NSLog(@"收到远程通知: %@",userInfo);
  212. } else {
  213. NSLog(@"收到本地通知: %@",userInfo);
  214. }
  215. // UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert
  216. completionHandler(UNNotificationPresentationOptionNone);
  217. }
  218. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  219. NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
  220. // 点击通知
  221. // NSDictionary *userInfo = response.notification.request.content.userInfo;
  222. // if (userInfo) {
  223. //
  224. // NSDictionary *aps = [userInfo objectForKey:@"aps"];
  225. //
  226. // NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue];
  227. // if (is_order) {
  228. // [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  229. // }
  230. // }
  231. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",[UIApplication sharedApplication].applicationState,__func__]];
  232. self.currentOrderNotification = nil;
  233. NSMutableDictionary *userInfo = [response.notification.request.content.userInfo mutableCopy];
  234. [userInfo setObject:@(YES) forKey:@"go2Detail"];
  235. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  236. completionHandler();
  237. }
  238. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  239. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",application.applicationState,__func__]];
  240. /**
  241. 静默推送alert为空
  242. {
  243. "aps" : {
  244. "alert" : "",
  245. "content-available" : 1
  246. }
  247. }
  248. */
  249. // "content-available" : 1 收到通知
  250. NSLog(@"收到静默推送: %@",userInfo);
  251. if (userInfo) {
  252. NSDictionary *aps = [userInfo objectForKey:@"aps"];
  253. NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue];
  254. if (report_location == 1) {
  255. NSString *orderID = [aps objectForKey:@"order-id"];
  256. BOOL tracing = [[aps objectForKey:@"tracing"] boolValue];
  257. NSString *locationId = [aps objectForKey:@"location-id"];
  258. if (tracing) {
  259. if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  260. [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  261. } else {
  262. [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Driver %@ rejected to report location",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  263. }
  264. return;
  265. }
  266. if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  267. [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  268. } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlwaysAsk) {
  269. [self askForReportLastLocation:orderID locationID:locationId];
  270. } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeReject) {
  271. [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Driver %@ rejected to report location",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  272. }
  273. completionHandler(UIBackgroundFetchResultNewData);
  274. } else {
  275. /**
  276. {
  277. "aps" : {
  278. "alert" : {
  279. "title" : "Notification",
  280. "body" : "You have a new order"
  281. },
  282. "is-order" : 1,
  283. "order-id" : "AFS20180530001200",
  284. "order-type2" : "Pick Up",
  285. "order-type" : 2,
  286. "sound" : "default",
  287. "content-available" : 1
  288. }
  289. }
  290. */
  291. NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue];
  292. if (is_order) {
  293. // 程序在前台,弹窗提示
  294. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
  295. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  296. } else {
  297. // 程序在后台,记录下来。当程序重新进入前台时弹窗提示(没有点击通知打开程序的情况)
  298. self.currentOrderNotification = userInfo;
  299. }
  300. }
  301. completionHandler(UIBackgroundFetchResultNewData);
  302. }
  303. } else {
  304. completionHandler(UIBackgroundFetchResultNoData);
  305. }
  306. }
  307. #pragma mark - Local Notification
  308. - (void)sendLocalNotification:(NSString *)title message:(NSString *)msg {
  309. // 创建Content
  310. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  311. content.title = title;
  312. content.body = msg;
  313. content.badge = @([UIApplication sharedApplication].applicationIconBadgeNumber + 1);
  314. // 创建Request,保证ID一致
  315. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"local_notification" content:content trigger:nil];
  316. // 发送
  317. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  318. if (!error) {
  319. NSLog(@"发送本地通知:%@",request.identifier);
  320. }
  321. }];
  322. }
  323. #pragma mark - Report Location
  324. - (void)rejectReportLocationWithReason:(NSString *)reason forOrder:(NSString *)orderID locationID:(NSString *)locationId {
  325. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  326. [RADataProvider reportLocationWithUserReason:reason forOrder:orderID locationID:locationId];
  327. });
  328. }
  329. - (void)reportLastLocation:(CLLocation *)location forOrder:(NSString *)orderID locationID:(NSString *)locationId {
  330. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  331. [RADataProvider reportLastLocation:location forOrderID:orderID locationID:locationId];
  332. });
  333. }
  334. - (void)askForReportLastLocation:(NSString *)orderID locationID:(NSString *)locationId{
  335. UIViewController *topVC = self.window.rootViewController;
  336. while (topVC.presentedViewController) {
  337. topVC = topVC.presentedViewController;
  338. }
  339. if (topVC) {
  340. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Report Location For Order:%@",orderID] preferredStyle:UIAlertControllerStyleAlert];
  341. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  342. [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Driver %@ cancel to report location",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  343. }];
  344. __weak typeof(self) weakSelf = self;
  345. UIAlertAction *reportAction = [UIAlertAction actionWithTitle:@"Report" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  346. [weakSelf reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  347. }];
  348. [alertVC addAction:cancelAction];
  349. [alertVC addAction:reportAction];
  350. [topVC presentViewController:alertVC animated:YES completion:nil];
  351. [self sendLocalNotification:@"Report Location Notification" message:[NSString stringWithFormat:@"The Apex ask your location for order:%@",orderID]];
  352. }
  353. }
  354. #pragma mark - LocationManager Delegate
  355. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  356. if (locations.count) {
  357. [RASingleton sharedInstance].currentLocation = [locations lastObject];
  358. [RASingleton sharedInstance].lastLocation = [locations lastObject];
  359. [RASingleton sharedInstance].lastLocationDateTime=[RAUtils current_date];
  360. NSLog(@"location: %f, %f",[RASingleton sharedInstance].currentLocation.coordinate.latitude,[RASingleton sharedInstance].currentLocation.coordinate.longitude);
  361. // 省电,停止不能超过三分钟
  362. // [self performSelector:@selector(stopLocation) withObject:nil afterDelay:10]; // 获取到位置10s后关闭位置服务
  363. // [self performSelector:@selector(startLocation) withObject:nil afterDelay:120]; // 获取到位置120s后重新打开位置服务
  364. //后台延迟定位3km,5分钟。
  365. CLLocationDistance distance = 500;
  366. NSTimeInterval time = 60;
  367. if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
  368. {
  369. // more aggressive when app is active
  370. distance = 20;
  371. time = 20;
  372. }
  373. // if(!manager.allowsBackgroundLocationUpdates)
  374. // {
  375. //
  376. // }
  377. [manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
  378. }
  379. }
  380. - (void)locationManager:(CLLocationManager *)manager
  381. didFailWithError:(NSError *)error
  382. {
  383. [RASingleton sharedInstance].currentLocation = nil;
  384. }
  385. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  386. if (status == kCLAuthorizationStatusDenied) {
  387. [RASingleton sharedInstance].currentLocation = nil;
  388. }else
  389. {
  390. [self.locationManager startUpdatingLocation];
  391. }
  392. }
  393. @end