AppDelegate.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // AppDelegate.m
  3. // RedAnt ERP Suite
  4. //
  5. // Created by Ray on 14-5-19.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "RAUtils.h"
  10. #import "RADataProvider.h"
  11. @implementation AppDelegate
  12. //@synthesize devices;
  13. -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  14. {
  15. RASingleton.sharedInstance.duid = [[[[NSString stringWithFormat:@"%@",deviceToken] substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)] stringByReplacingOccurrencesOfString:@" " withString:@""];
  16. NSLog(@"regisger success:%@",RASingleton.sharedInstance.duid);
  17. const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
  18. NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
  19. ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
  20. ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
  21. ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
  22. NSLog(@">=ios13 My FINAL TOKEN is:%@", strToken);
  23. RASingleton.sharedInstance.duid =strToken;
  24. //注册成功,将deviceToken保存到应用服务器数据库中
  25. }
  26. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  27. {
  28. BOOL RET= [super application:application didFinishLaunchingWithOptions:launchOptions];
  29. //消息推送注册
  30. UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
  31. [center setDelegate:self];
  32. UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
  33. [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
  34. if (granted) {
  35. NSLog(@"推送通知授权成功");
  36. }else{
  37. NSLog(@"推送通知授权失败");
  38. }
  39. }];
  40. [application registerForRemoteNotifications];
  41. return RET;
  42. }
  43. - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
  44. {
  45. NSLog(@"Failed to get token, error: %@", error);
  46. }
  47. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  48. if(self.occupied)
  49. {
  50. completionHandler(UIBackgroundFetchResultNoData);
  51. return; // 不处理后台时的推送,避免重复推送
  52. }
  53. if(application.applicationState == UIApplicationStateInactive)
  54. {
  55. completionHandler(UIBackgroundFetchResultNoData);
  56. return; // 不处理后台时的推送,避免重复推送
  57. }
  58. self.occupied = true;
  59. UIViewController *vc = [RAUtils getCurrentVC];
  60. NSString * name=NSStringFromClass(vc.class);
  61. bool blogin = true;
  62. if(RASingleton.sharedInstance.user.length==0||[RASingleton.sharedInstance.user.lowercaseString isEqualToString:@"guest"])
  63. blogin = false;
  64. // int a=0;
  65. bool bupdate = false; //是否有更新按钮
  66. if([name isEqualToString:@"CreateOrderViewController"]||[name isEqualToString:@"StockViewController"])
  67. {
  68. bupdate = false;
  69. }
  70. else
  71. {
  72. bupdate = true;
  73. }
  74. bool berp = false; //是否是erp用户
  75. NSString* servername =nil;
  76. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];;
  77. if (addressDic) {
  78. berp=[addressDic[@"has_erp_site"] boolValue];
  79. servername=addressDic[@"name"];
  80. }
  81. if(![userInfo[@"aps"][@"server"] isEqualToString:servername])
  82. {
  83. completionHandler(UIBackgroundFetchResultNoData);
  84. return; // 只处理来自当前server的消息,不是当前服务器,返回。
  85. }
  86. // 取消所有推送
  87. [application setApplicationIconBadgeNumber:0];
  88. // UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
  89. // [center removeAllPendingNotificationRequests];
  90. // 弹出提示框
  91. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Local Data Update Required" message:nil preferredStyle:UIAlertControllerStyleAlert];
  92. //block代码块取代了delegate
  93. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Update Local Data" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  94. [RADataProvider updateTemplate:vc];
  95. self.occupied = false;
  96. }];
  97. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Update Local Data and Inventory" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  98. [RADataProvider updateTemplate:vc];
  99. [RADataProvider updateStock:vc];
  100. self.occupied = false;
  101. }];
  102. if(bupdate)
  103. [alertControl addAction:actionOne];
  104. if(bupdate && berp && blogin)
  105. [alertControl addAction:alertthree];
  106. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Update Later" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  107. self.occupied = false;
  108. }];
  109. [alertControl addAction:alertcancel];
  110. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  111. [vc presentViewController:alertControl animated:YES completion:nil];
  112. completionHandler(UIBackgroundFetchResultNewData);
  113. //
  114. //// [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",application.applicationState,__func__]];
  115. // /**
  116. // 静默推送alert为空
  117. // {
  118. // "aps" : {
  119. // "alert" : "",
  120. // "content-available" : 1
  121. // }
  122. // }
  123. // */
  124. //
  125. // // "content-available" : 1 收到通知
  126. // NSLog(@"收到静默推送: %@",userInfo);
  127. // if (userInfo) {
  128. //
  129. // NSDictionary *aps = [userInfo objectForKey:@"aps"];
  130. //// [UIApplication sharedApplication].applicationIconBadgeNumber = [aps[@"badge"] intValue];
  131. // NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue];
  132. // if (report_location == 1) {
  133. //
  134. // NSString *orderID = [aps objectForKey:@"order-id"];
  135. // BOOL tracing = [[aps objectForKey:@"tracing"] boolValue];
  136. // NSString *locationId = [aps objectForKey:@"location-id"];
  137. //
  138. // if (tracing) {
  139. //
  140. // if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  141. //
  142. // [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  143. //
  144. // } else {
  145. //
  146. // [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject tracing by app setting Driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  147. // }
  148. //
  149. // return;
  150. // }
  151. //
  152. // if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  153. //
  154. // [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  155. //
  156. // } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlwaysAsk) {
  157. //
  158. // [self askForReportLastLocation:orderID locationID:locationId];
  159. // } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeReject) {
  160. //
  161. // [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject ping by app setting,driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  162. // }
  163. // completionHandler(UIBackgroundFetchResultNewData);
  164. // } else {
  165. // /**
  166. // {
  167. // "aps" : {
  168. // "alert" : {
  169. // "title" : "Notification",
  170. // "body" : "You have a new order"
  171. // },
  172. // "is-order" : 1,
  173. // "order-id" : "AFS20180530001200",
  174. // "order-type2" : "Pick Up",
  175. // "order-type" : 2,
  176. // "sound" : "default",
  177. // "content-available" : 1
  178. // }
  179. // }
  180. // */
  181. //
  182. // NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue];
  183. // if (is_order) {
  184. //
  185. // // 程序在前台,弹窗提示
  186. // if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
  187. //
  188. // [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  189. //
  190. // } else {
  191. // // 程序在后台,记录下来。当程序重新进入前台时弹窗提示(没有点击通知打开程序的情况)
  192. // self.currentOrderNotification = userInfo;
  193. // }
  194. // }
  195. // completionHandler(UIBackgroundFetchResultNewData);
  196. // }
  197. // } else {
  198. // completionHandler(UIBackgroundFetchResultNoData);
  199. // }
  200. }
  201. @end