AppDelegate.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. @interface AppDelegate ()<UNUserNotificationCenterDelegate,CLLocationManagerDelegate>
  14. @property (nonatomic,weak) UINavigationController *rootVC;
  15. @property (nonatomic,strong) CLLocationManager *locationManager;
  16. @property (nonatomic,strong) CLLocation *currentLocation;
  17. @end
  18. @implementation AppDelegate
  19. #pragma mark - Private
  20. - (void)showHomeVC {
  21. RAHomeViewController *homeVC = [RAHomeViewController viewControllerFromStoryboard];
  22. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homeVC];
  23. self.window.rootViewController = nav;
  24. }
  25. - (void)saveUploadTasks {
  26. if(self.uploadManager.arr_queue!=nil)
  27. {
  28. [self.uploadManager stopAllTasks];
  29. [self.uploadManager saveTasks];
  30. }
  31. }
  32. - (void)startLocation {
  33. if (self.locationManager) {
  34. return;
  35. }
  36. self.locationManager = [[CLLocationManager alloc] init];
  37. self.locationManager.delegate = self;
  38. self.locationManager.allowsBackgroundLocationUpdates = YES;
  39. self.locationManager.pausesLocationUpdatesAutomatically = NO; // 是否允许系统自动暂停定位
  40. self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//设置定位精度
  41. self.locationManager.distanceFilter = kCLDistanceFilterNone; //不需要移动都可以刷新,不然不移动就不会执行定位,不定位的话,那么后台进程也就挂起了
  42. [self.locationManager requestAlwaysAuthorization];
  43. [self.locationManager startUpdatingLocation];
  44. }
  45. - (void)stopLocation {
  46. if (self.locationManager == nil) {
  47. return;
  48. }
  49. [self.locationManager stopUpdatingLocation];
  50. self.locationManager = nil;
  51. }
  52. - (void)reportLocation:(CLLocation *)location {
  53. if (location) {
  54. } else {
  55. }
  56. }
  57. #pragma mark - AppDelegate
  58. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  59. // Override point for customization after application launch.
  60. // View
  61. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  62. self.window.backgroundColor = [UIColor whiteColor];
  63. LoginViewController *rootVC = [LoginViewController viewControllerFromStoryboard];
  64. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
  65. self.window.rootViewController = nav;
  66. __weak typeof(self) weakSelf = self;
  67. rootVC.loginSuccessful = ^{
  68. [weakSelf showHomeVC];
  69. };
  70. [self.window makeKeyAndVisible];
  71. [self startLocation];
  72. //消息推送注册
  73. UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
  74. [center setDelegate:self];
  75. UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
  76. [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
  77. if (granted) {
  78. NSLog(@"推送通知授权成功");
  79. }else{
  80. NSLog(@"推送通知授权失败");
  81. }
  82. }];
  83. [application registerForRemoteNotifications];
  84. return YES;
  85. }
  86. - (void)applicationDidBecomeActive:(UIApplication *)application {
  87. if (!self.uploadManager) {
  88. self.uploadManager=[[RAUploadManager alloc] init];
  89. }
  90. }
  91. - (void)applicationWillTerminate:(UIApplication *)application {
  92. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  93. [self saveUploadTasks];
  94. }
  95. #pragma mark - Notification
  96. //完成注册后收到devicetoken
  97. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  98. //上传deviceToken给后台服务器
  99. NSString *fullStr = [deviceToken description];
  100. NSLog(@"fullstr= %@",fullStr);
  101. NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
  102. NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
  103. }
  104. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  105. NSLog(@"注册推送失败:%@",error);
  106. }
  107. //处理通知
  108. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  109. // 程序在前台收到通知
  110. UNNotificationRequest *request = notification.request;
  111. NSDictionary *userInfo = request.content.userInfo;
  112. if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  113. NSLog(@"收到远程通知: %@",userInfo);
  114. } else {
  115. NSLog(@"收到本地通知: %@",userInfo);
  116. }
  117. // UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert
  118. completionHandler(UNNotificationPresentationOptionNone);
  119. }
  120. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  121. NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
  122. // 点击通知
  123. completionHandler();
  124. }
  125. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  126. /**
  127. 静默推送alert为空
  128. {
  129. "aps" : {
  130. "alert" : "",
  131. "content-available" : 1
  132. }
  133. }
  134. */
  135. // "content-available" : 1 收到通知
  136. NSLog(@"收到静默推送: %@",userInfo);
  137. if (userInfo) {
  138. NSDictionary *aps = [userInfo objectForKey:@"aps"];
  139. NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue];
  140. if (report_location == 1) {
  141. [self reportLocation:self.currentLocation];
  142. completionHandler(UIBackgroundFetchResultNewData);
  143. } else {
  144. /**
  145. {
  146. "aps" : {
  147. "alert" : {
  148. "title" : "Notification",
  149. "body" : "You have a new order"
  150. },
  151. "new-order" : 1,
  152. "sound" : "default",
  153. "content-available" : 1
  154. }
  155. }
  156. */
  157. NSInteger new_order = [[aps objectForKey:@"new-order"] integerValue];
  158. if (new_order) {
  159. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationNewOrder object:nil];
  160. }
  161. completionHandler(UIBackgroundFetchResultNewData);
  162. }
  163. } else {
  164. completionHandler(UIBackgroundFetchResultNoData);
  165. }
  166. }
  167. #pragma mark - LocationManager Delegate
  168. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  169. if (locations.count) {
  170. self.currentLocation = [locations lastObject];
  171. NSLog(@"location: %f, %f",self.currentLocation.coordinate.latitude,self.currentLocation.coordinate.longitude);
  172. // 省电,停止不能超过三分钟
  173. // [self performSelector:@selector(stopLocation) withObject:nil afterDelay:10]; // 获取到位置10s后关闭位置服务
  174. // [self performSelector:@selector(startLocation) withObject:nil afterDelay:120]; // 获取到位置120s后重新打开位置服务
  175. CLLocationDistance distance = 500;
  176. NSTimeInterval time = 60;
  177. [manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
  178. }
  179. }
  180. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  181. if (status == kCLAuthorizationStatusDenied) {
  182. self.currentLocation = nil;
  183. }
  184. }
  185. @end