AppDelegate.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. //
  3. // AppDelegate.m
  4. // Haishenghai-master
  5. //
  6. // Created by GG on 2018/12/29.
  7. // Copyright © 2018年 Haishenghai intelligence network technology. All rights reserved.
  8. //
  9. #import "AppDelegate.h"
  10. #import "Header.h"
  11. #import "EditLocationVC.h"
  12. //引入JPush功能所需要的头文件
  13. #import <JPUSHService.h>
  14. //iOS 10注册APNS 所需头文件
  15. //#ifdef NSFoundationVersionNumber_iOS_9_x_Max
  16. #import <UserNotifications/UserNotifications.h>
  17. //如果需要使用idfa 功能所需要引入的头文件(可选)
  18. #import <AdSupport/AdSupport.h>
  19. //mapKey:6f1d673114becda95d8a07d8b163f929
  20. //极光推送appKey:fe13061435f5517e3c551231
  21. //给JPush添加注册通知代理
  22. #import "FunSDK/FunSDK.h"
  23. #import "FunSDK/netsdk.h"
  24. #import "SDKInitializeModel.h"
  25. #import "DeviceManager.h"
  26. #import "MainViewController.h"
  27. #import "XYShowAlertView.h"
  28. @interface AppDelegate ()<JPUSHRegisterDelegate,JPUSHGeofenceDelegate>
  29. @end
  30. @implementation AppDelegate
  31. static NSInteger seq = 0;
  32. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  33. //启动时 注册FunSDK
  34. [SDKInitializeModel SDKInit];
  35. // Override point for customization after application launch.
  36. [self monitorNetworking];
  37. ////// 跳转到登录
  38. [AppDelegate LoginRootViewController];
  39. // [self goMain];
  40. //测试入口
  41. // EditLocationVC *loginVC = [[EditLocationVC alloc]init];
  42. // UINavigationController *navgation = [[UINavigationController alloc]initWithRootViewController:loginVC];
  43. // self.window.rootViewController = navgation;
  44. // loginVC.navigationController.navigationBarHidden = YES;
  45. //--------------------------------------------------------------------------------------------
  46. // 高德地图SDK
  47. //---------------------------------------------------------------------------------------------
  48. [AMapServices sharedServices].apiKey = @"6f1d673114becda95d8a07d8b163f929";
  49. //
  50. //单次定位
  51. [self.window makeKeyAndVisible];
  52. //--------------------------------------------------------------------------------------------
  53. // 极光推送SDK
  54. //---------------------------------------------------------------------------------------------
  55. // 3.0.0及以后版本注册
  56. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  57. if (@available(iOS 12.0, *)) {
  58. entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
  59. } else {
  60. entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
  61. }
  62. NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  63. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  64. [JPUSHService registerLbsGeofenceDelegate:self withLaunchOptions:launchOptions];
  65. //如不需要使用IDFA,advertisingIdentifier 可为nil
  66. [JPUSHService setupWithOption:launchOptions appKey:appKey
  67. channel:channel
  68. apsForProduction:isProduction
  69. advertisingIdentifier:advertisingId];
  70. NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
  71. NSLog(@"------%@",remoteNotification);
  72. NSString *LoginID = [[NSUserDefaults standardUserDefaults]objectForKey:@"loginId"];
  73. [JPUSHService setAlias:LoginID completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
  74. [self inputResponseCode:iResCode content:iAlias andSeq:seq];
  75. } seq:[self seq]];
  76. return YES;
  77. }
  78. - (void)inputResponseCode:(NSInteger)code content:(NSString *)content andSeq:(NSInteger)seq{
  79. NSLog (@"\n\n code:%ld content:%@ seq:%ld", code, content, seq);
  80. }
  81. - (NSInteger)seq {
  82. return ++ seq;
  83. }
  84. +(void)LoginRootViewController{
  85. UserLogin_VC *loginVC = [[UserLogin_VC alloc]init];
  86. UINavigationController *navgation = [[UINavigationController alloc]initWithRootViewController:loginVC];
  87. AppDelegate *delegate = (id)[UIApplication sharedApplication].delegate;
  88. delegate.window.rootViewController = navgation;
  89. loginVC.navigationController.navigationBarHidden = YES;
  90. }
  91. -(void)playSound{
  92. // 播放系统声音
  93. //定义一个SystemSoundID
  94. // SystemSoundID soundID = 1331;
  95. //// 播放声音
  96. // AudioServicesPlaySystemSound(soundID);
  97. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fireAlarm" ofType:@"mp3"];
  98. NSURL *fileUrl = [NSURL URLWithString:filePath];
  99. self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
  100. //设置声音的大小
  101. self.player.volume =1;
  102. //设置循环次数 ,如果为负数,就是无限循环
  103. self.player.numberOfLoops = -1;
  104. //准备播放
  105. [self.player prepareToPlay];
  106. //播放
  107. [self.player play];
  108. }
  109. #pragma mark- JPUSHRegisterDelegate
  110. // iOS 12 Support
  111. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
  112. NSLog(@"1111111111111");
  113. if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  114. [[NSNotificationCenter defaultCenter]postNotificationName:@"fireChange" object:nil];
  115. //从通知界面直接进入应用
  116. // ViewController *view = [[ViewController alloc]init];
  117. // self.window.rootViewController = view;
  118. }else{
  119. //从通知设置界面进入应用
  120. }
  121. }
  122. // iOS 10 Support
  123. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  124. NSLog(@"2222222222");
  125. // [self playSound];
  126. // [[NSNotificationCenter defaultCenter]postNotificationName:@"fireChange" object:nil];
  127. // //从通知界面直接进入应用
  128. // ViewController *view = [[ViewController alloc]init];
  129. // self.window.rootViewController = view;
  130. // Required
  131. NSDictionary * userInfo = notification.request.content.userInfo;
  132. NSLog(@"------------->%@",userInfo);
  133. NSLog(@"------------->%@",notification.request);
  134. UNNotificationRequest *request = notification.request; // 收到推送的请求
  135. UNNotificationContent *content = request.content; // 收到推送的消息内容
  136. NSNumber *badge = content.badge; // 推送消息的角标
  137. NSString *body = content.body; // 推送消息体
  138. UNNotificationSound *sound = content.sound; // 推送消息的声音
  139. NSString *subtitle = content.subtitle; // 推送消息的副标题
  140. NSString *title = content.title; // 推送消息的标题
  141. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  142. [JPUSHService handleRemoteNotification:userInfo];
  143. }
  144. completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  145. [[NSNotificationCenter defaultCenter]postNotificationName:@"fireChange" object:nil];
  146. //从通知界面直接进入应用
  147. // ViewController *view = [[ViewController alloc]init];
  148. // self.window.rootViewController = view;
  149. }
  150. // iOS 10 Support
  151. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  152. NSLog(@"33333333333");
  153. // Required
  154. NSDictionary * userInfo = response.notification.request.content.userInfo;
  155. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  156. [JPUSHService handleRemoteNotification:userInfo];
  157. NSLog(@"iOS10 收到远程通知:%@", userInfo);
  158. // [[NSNotificationCenter defaultCenter]postNotificationName:@"fireChange" object:nil];
  159. // //从通知界面直接进入应用
  160. // ViewController *view = [[ViewController alloc]init];
  161. // self.window.rootViewController = view;
  162. }
  163. completionHandler(); // 系统要求执行这个方法
  164. [[NSNotificationCenter defaultCenter]postNotificationName:@"fireChange" object:nil];
  165. //从通知界面直接进入应用
  166. // ViewController *view = [[ViewController alloc]init];
  167. // self.window.rootViewController = view;
  168. }
  169. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  170. NSLog(@"44444444444");
  171. [[NSNotificationCenter defaultCenter]postNotificationName:@"appQuitChange" object:nil];
  172. //从通知界面直接进入应用
  173. ViewController *view = [[ViewController alloc]init];
  174. self.window.rootViewController = view;
  175. // 取得 APNs 标准信息内容
  176. NSDictionary *aps = [userInfo valueForKey:@"aps"];
  177. NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
  178. NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge 数量
  179. NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
  180. // 取得 Extras 字段内容
  181. NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服务端中 Extras 字段,key 是自己定义的
  182. NSLog(@"content =[%@], badge=[%d], sound=[%@], customize field =[%@]",content,badge,sound,customizeField1);
  183. // iOS 10 以下 Required
  184. [JPUSHService handleRemoteNotification:userInfo];
  185. }
  186. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  187. // 取得 APNs 标准信息内容
  188. // NSLog(@"_______-%@",userInfo);
  189. NSLog(@"55555555555");
  190. [self playSound];
  191. // Required, iOS 7 Support
  192. [JPUSHService handleRemoteNotification:userInfo];
  193. [[NSNotificationCenter defaultCenter]postNotificationName:@"appQuitChange" object:nil];
  194. //从通知界面直接进入应用
  195. ViewController *view = [[ViewController alloc]init];
  196. self.window.rootViewController = view;
  197. completionHandler(UIBackgroundFetchResultNewData);
  198. }
  199. //检测网络状态
  200. #pragma mark - ------------- 监测网络状态 -------------
  201. - (void)monitorNetworking
  202. {
  203. //此方法不能用,否则改变了根视图控制器的指向
  204. // UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  205. // alertWindow.rootViewController = [[UIViewController alloc] init];
  206. // alertWindow.windowLevel = UIWindowLevelAlert + 1;
  207. // [alertWindow makeKeyAndVisible];
  208. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  209. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  210. switch (status) {
  211. case AFNetworkReachabilityStatusUnknown:
  212. NSLog(@"未知网络");
  213. break;
  214. case AFNetworkReachabilityStatusNotReachable:
  215. {
  216. NSLog(@"无网络连接");
  217. //跳转到无网络界面
  218. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  219. [alert show];
  220. }
  221. break;
  222. case AFNetworkReachabilityStatusReachableViaWWAN:
  223. {
  224. NSLog(@"GPRS网络");
  225. }
  226. break;
  227. case AFNetworkReachabilityStatusReachableViaWiFi:
  228. {
  229. NSLog(@"wifi网络");
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. }];
  236. }
  237. //注册deviceToken
  238. - (void)application:(UIApplication *)application
  239. didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  240. /// Required - 注册 DeviceToken
  241. [JPUSHService registerDeviceToken:deviceToken];
  242. }
  243. //注册APNS失败的接口
  244. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  245. //Optional
  246. NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
  247. }
  248. - (void)applicationWillResignActive:(UIApplication *)application {
  249. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  250. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  251. }
  252. - (void)applicationDidEnterBackground:(UIApplication *)application {
  253. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  254. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  255. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  256. // UIApplication* app = [UIApplication sharedApplication];
  257. // __block UIBackgroundTaskIdentifier bgTask;
  258. // bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
  259. // dispatch_async(dispatch_get_main_queue(), ^{
  260. // if (bgTask != UIBackgroundTaskInvalid)
  261. // {
  262. // bgTask = UIBackgroundTaskInvalid;
  263. // }
  264. // });
  265. // }];
  266. //
  267. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  268. // dispatch_async(dispatch_get_main_queue(), ^{
  269. // if (bgTask != UIBackgroundTaskInvalid)
  270. // {
  271. // bgTask = UIBackgroundTaskInvalid;
  272. // }
  273. // });
  274. // });
  275. }
  276. - (void)applicationWillEnterForeground:(UIApplication *)application {
  277. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  278. [application setApplicationIconBadgeNumber:0];
  279. [application cancelAllLocalNotifications];
  280. }
  281. - (void)applicationDidBecomeActive:(UIApplication *)application {
  282. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  283. }
  284. - (void)applicationWillTerminate:(UIApplication *)application {
  285. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  286. }
  287. @end