// // AppDelegate.m // RedAnt ERP Suite // // Created by Ray on 14-5-19. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved. // #import "AppDelegate.h" #import "RAUtils.h" #import "RADataProvider.h" @implementation AppDelegate //@synthesize devices; -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { RASingleton.sharedInstance.duid = [[[[NSString stringWithFormat:@"%@",deviceToken] substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)] stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"regisger success:%@",RASingleton.sharedInstance.duid); const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes]; NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]), ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]), ntohl(tokenBytes[6]), ntohl(tokenBytes[7])]; NSLog(@">=ios13 My FINAL TOKEN is:%@", strToken); RASingleton.sharedInstance.duid =strToken; //注册成功,将deviceToken保存到应用服务器数据库中 } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { BOOL RET= [super application:application didFinishLaunchingWithOptions:launchOptions]; //消息推送注册 UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; [center setDelegate:self]; UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert; [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"推送通知授权成功"); }else{ NSLog(@"推送通知授权失败"); } }]; [application registerForRemoteNotifications]; return RET; } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"Failed to get token, error: %@", error); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if(application.applicationState == UIApplicationStateInactive) { completionHandler(UIBackgroundFetchResultNoData); return; // 不处理后台时的推送,避免重复推送 } UIViewController *vc = [RAUtils getCurrentVC]; NSString * name=NSStringFromClass(vc.class); bool blogin = true; if(self.user.length==0||[self.user.lowercaseString isEqualToString:@"guest"]) blogin = false; // int a=0; bool bupdate = false; //是否有更新按钮 if([name isEqualToString:@"CreateOrderViewController"]||[name isEqualToString:@"StockViewController"]) { bupdate = false; } else { bupdate = true; } bool berp = false; //是否是erp用户 NSString* servername =nil; NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];; if (addressDic) { berp=[addressDic[@"has_erp_site"] boolValue]; servername=addressDic[@"name"]; } if(![userInfo[@"aps"][@"server"] isEqualToString:servername]) { completionHandler(UIBackgroundFetchResultNoData); return; // 只处理来自当前server的消息,不是当前服务器,返回。 } UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Local Data Update Required" message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Update Local Data" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [RADataProvider updateTemplate:vc]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Update Local Data and Inventory" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [RADataProvider updateTemplate:vc]; [RADataProvider updateStock:vc]; }]; if(bupdate) [alertControl addAction:actionOne]; if(bupdate && berp && blogin) [alertControl addAction:alertthree]; UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Update Later" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:alertcancel]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [vc presentViewController:alertControl animated:YES completion:nil]; completionHandler(UIBackgroundFetchResultNewData); // //// [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"isActive: %ld %s",application.applicationState,__func__]]; // /** // 静默推送alert为空 // { // "aps" : { // "alert" : "", // "content-available" : 1 // } // } // */ // // // "content-available" : 1 收到通知 // NSLog(@"收到静默推送: %@",userInfo); // if (userInfo) { // // NSDictionary *aps = [userInfo objectForKey:@"aps"]; //// [UIApplication sharedApplication].applicationIconBadgeNumber = [aps[@"badge"] intValue]; // NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue]; // if (report_location == 1) { // // NSString *orderID = [aps objectForKey:@"order-id"]; // BOOL tracing = [[aps objectForKey:@"tracing"] boolValue]; // NSString *locationId = [aps objectForKey:@"location-id"]; // // if (tracing) { // // if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) { // // [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId]; // // } else { // // [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject tracing by app setting Driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId]; // } // // return; // } // // if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) { // // [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID locationID:locationId]; // // } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlwaysAsk) { // // [self askForReportLastLocation:orderID locationID:locationId]; // } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeReject) { // // [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Reject ping by app setting,driver: %@",RASingleton.sharedInstance.user] forOrder:orderID locationID:locationId]; // } // completionHandler(UIBackgroundFetchResultNewData); // } else { // /** // { // "aps" : { // "alert" : { // "title" : "Notification", // "body" : "You have a new order" // }, // "is-order" : 1, // "order-id" : "AFS20180530001200", // "order-type2" : "Pick Up", // "order-type" : 2, // "sound" : "default", // "content-available" : 1 // } // } // */ // // NSInteger is_order = [[aps objectForKey:@"is-order"] integerValue]; // if (is_order) { // // // 程序在前台,弹窗提示 // if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { // // [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationHandleOrder object:nil userInfo:userInfo]; // // } else { // // 程序在后台,记录下来。当程序重新进入前台时弹窗提示(没有点击通知打开程序的情况) // self.currentOrderNotification = userInfo; // } // } // completionHandler(UIBackgroundFetchResultNewData); // } // } else { // completionHandler(UIBackgroundFetchResultNoData); // } } @end