AppDelegate.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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(application.applicationState == UIApplicationStateInactive)
  49. {
  50. completionHandler(UIBackgroundFetchResultNoData);
  51. return; // 不处理后台时的推送,避免重复推送
  52. }
  53. UIViewController *vc = [RAUtils getCurrentVC];
  54. NSString * name=NSStringFromClass(vc.class);
  55. bool blogin = true;
  56. if(self.user.length==0||[self.user.lowercaseString isEqualToString:@"guest"])
  57. blogin = false;
  58. // int a=0;
  59. bool bupdate = false; //是否有更新按钮
  60. if([name isEqualToString:@"CreateOrderViewController"]||[name isEqualToString:@"StockViewController"])
  61. {
  62. bupdate = false;
  63. }
  64. else
  65. {
  66. bupdate = true;
  67. }
  68. bool berp = false; //是否是erp用户
  69. NSString* servername =nil;
  70. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];;
  71. if (addressDic) {
  72. berp=[addressDic[@"has_erp_site"] boolValue];
  73. servername=addressDic[@"name"];
  74. }
  75. if(![userInfo[@"aps"][@"server"] isEqualToString:servername])
  76. {
  77. completionHandler(UIBackgroundFetchResultNoData);
  78. return; // 只处理来自当前server的消息,不是当前服务器,返回。
  79. }
  80. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Local Data Update Required" message:nil preferredStyle:UIAlertControllerStyleAlert];
  81. //block代码块取代了delegate
  82. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Update Local Data" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  83. [RADataProvider updateTemplate:vc];
  84. }];
  85. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Update Local Data and Inventory" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  86. [RADataProvider updateTemplate:vc];
  87. [RADataProvider updateStock:vc];
  88. }];
  89. if(bupdate)
  90. [alertControl addAction:actionOne];
  91. if(bupdate && berp && blogin)
  92. [alertControl addAction:alertthree];
  93. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Update Later" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  94. }];
  95. [alertControl addAction:alertcancel];
  96. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  97. [vc presentViewController:alertControl animated:YES completion:nil];
  98. completionHandler(UIBackgroundFetchResultNewData);
  99. //
  100. //// [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",application.applicationState,__func__]];
  101. // /**
  102. // 静默推送alert为空
  103. // {
  104. // "aps" : {
  105. // "alert" : "",
  106. // "content-available" : 1
  107. // }
  108. // }
  109. // */
  110. //
  111. // // "content-available" : 1 收到通知
  112. // NSLog(@"收到静默推送: %@",userInfo);
  113. // if (userInfo) {
  114. //
  115. // NSDictionary *aps = [userInfo objectForKey:@"aps"];
  116. //// [UIApplication sharedApplication].applicationIconBadgeNumber = [aps[@"badge"] intValue];
  117. // NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue];
  118. // if (report_location == 1) {
  119. //
  120. // NSString *orderID = [aps objectForKey:@"order-id"];
  121. // BOOL tracing = [[aps objectForKey:@"tracing"] boolValue];
  122. // NSString *locationId = [aps objectForKey:@"location-id"];
  123. //
  124. // if (tracing) {
  125. //
  126. // if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  127. //
  128. // [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  129. //
  130. // } else {
  131. //
  132. // [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject tracing by app setting Driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  133. // }
  134. //
  135. // return;
  136. // }
  137. //
  138. // if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
  139. //
  140. // [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId];
  141. //
  142. // } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlwaysAsk) {
  143. //
  144. // [self askForReportLastLocation:orderID locationID:locationId];
  145. // } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeReject) {
  146. //
  147. // [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject ping by app setting,driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId];
  148. // }
  149. // completionHandler(UIBackgroundFetchResultNewData);
  150. // } else {
  151. // /**
  152. // {
  153. // "aps" : {
  154. // "alert" : {
  155. // "title" : "Notification",
  156. // "body" : "You have a new order"
  157. // },
  158. // "is-order" : 1,
  159. // "order-id" : "AFS20180530001200",
  160. // "order-type2" : "Pick Up",
  161. // "order-type" : 2,
  162. // "sound" : "default",
  163. // "content-available" : 1
  164. // }
  165. // }
  166. // */
  167. //
  168. // NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue];
  169. // if (is_order) {
  170. //
  171. // // 程序在前台,弹窗提示
  172. // if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
  173. //
  174. // [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo];
  175. //
  176. // } else {
  177. // // 程序在后台,记录下来。当程序重新进入前台时弹窗提示(没有点击通知打开程序的情况)
  178. // self.currentOrderNotification = userInfo;
  179. // }
  180. // }
  181. // completionHandler(UIBackgroundFetchResultNewData);
  182. // }
  183. // } else {
  184. // completionHandler(UIBackgroundFetchResultNoData);
  185. // }
  186. }
  187. @end