Ver Fonte

1.修改iOS Apex & Drivers通知Token处理,以及新订单打开。

Pen Li há 8 anos atrás
pai
commit
f7c3a487b7

+ 27 - 6
Redant Drivers/Apex And Drivers/AppDelegate.m

@@ -21,9 +21,10 @@
 
 #pragma mark - Private
 
-- (void)showHomeVC {
+- (void)showHomeVCWithOrderID:(NSString *)orderID {
     
     RAHomeViewController *homeVC = [RAHomeViewController viewControllerFromStoryboard];
+    homeVC.gotoDetailID = orderID;
     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homeVC];
     self.window.rootViewController = nav;
 }
@@ -35,11 +36,9 @@
     __weak typeof(self) weakSelf = self;
     rootVC.loginSuccessful = ^(NSString *user,NSString *password){
         
-        RASingleton.sharedInstance.user = user;
-        RASingleton.sharedInstance.password = password;
-        [RASingleton.sharedInstance saveUserInfo];
+        [[RASingleton sharedInstance] loginUser:user password:password];
         
-        [weakSelf showHomeVC];
+        [weakSelf showHomeVCWithOrderID:nil];
     };
     
     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
@@ -103,12 +102,23 @@
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     // Override point for customization after application launch.
     
+    NSString *orderID = nil;
+    if (launchOptions) {
+        NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
+        NSDictionary *aps = [userInfo objectForKey:@"aps"];
+        orderID = [aps objectForKey:@"order-id"];
+    }
+    
     // View
     
     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
     self.window.backgroundColor = [UIColor whiteColor];
     
-    [self showLoginVC];
+    if (RASingleton.sharedInstance.autoLogin) {
+        [self showHomeVCWithOrderID:orderID];
+    } else {
+        [self showLoginVC];
+    }
     
     [self.window makeKeyAndVisible];
     
@@ -130,6 +140,7 @@
     }];
     [application registerForRemoteNotifications];
     
+    
     return YES;
 }
 
@@ -154,6 +165,8 @@
     NSLog(@"fullstr= %@",fullStr);
     NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
     NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
+    
+    [[RASingleton sharedInstance] setNotificationToken:deviceTokenStr];
 }
 
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
@@ -180,6 +193,13 @@
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
     NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
     // 点击通知
+    NSDictionary *userInfo = response.notification.request.content.userInfo;
+    NSDictionary *aps = [userInfo objectForKey:@"aps"];
+    NSString *orderID = [aps objectForKey:@"order-id"];
+    if (orderID) {
+        [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationGoDetail object:@{@"orderID" : orderID}];
+    }
+    
     completionHandler();
 }
 
@@ -214,6 +234,7 @@
                     "body" : "You have a new order"
                     },
                 "new-order" : 1,
+                "order-id" : "AFS20180530001200",
                 "sound" : "default",
                 "content-available" : 1
                 }

+ 2 - 0
Redant Drivers/Apex And Drivers/Home/RAHomeViewController.h

@@ -15,6 +15,8 @@
 
 @property (nonatomic,copy) NSString *currentOrderID;
 
+@property (nonatomic,copy) NSString *gotoDetailID;
+
 #pragma mark - Getter
 
 - (NSUInteger)orderSectionCount;

+ 47 - 2
Redant Drivers/Apex And Drivers/Home/RAHomeViewController.m

@@ -98,7 +98,15 @@
 
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
+}
+
+- (void)viewDidAppear:(BOOL)animated {
+    [super viewDidAppear:animated];
     
+    if (self.gotoDetailID) {
+        [self pushDetailViewControllerForOrderID:self.gotoDetailID];
+        self.gotoDetailID = nil;
+    }
 }
 
 - (void)dealloc {
@@ -146,6 +154,7 @@
 - (void)registNotification {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationNewOrder object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationReloadHome object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveGoDetailNotification:) name:RANotificationGoDetail object:nil];
 }
 
 #pragma mark - Action
@@ -157,7 +166,25 @@
 }
 
 - (void)logoutItemClick:(UIBarButtonItem *)sender {
-    [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
+    
+    // show progress
+    RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
+    
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+        
+        [RADataProvider logout];
+        
+        dispatch_async(dispatch_get_main_queue(), ^{
+            
+            // dismiss progress
+            [hud dismiss];
+            
+            [RASingleton.sharedInstance logout];
+            [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
+            
+        });
+        
+    });
 }
 
 - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
@@ -266,8 +293,16 @@
 
 - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
     
+    [self pushDetailViewControllerForOrderID:model.orderID];
+}
+
+- (void)pushDetailViewControllerForOrderID:(NSString *)orderID {
+    
+    if (!orderID) {
+        return;
+    }
     RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
-    detailVC.orderID = model.orderID;
+    detailVC.orderID = orderID;
     [self.navigationController pushViewController:detailVC animated:YES];
 }
 
@@ -288,4 +323,14 @@
     [self loadData];
 }
 
+- (void)receiveGoDetailNotification:(NSNotification *)notification {
+    NSDictionary *userInfo = notification.object;
+    if (userInfo) {
+        NSString *orderID = [userInfo objectForKey:@"orderID"];
+        if (orderID) {
+            [self pushDetailViewControllerForOrderID:orderID];
+        }
+    }
+}
+
 @end

+ 4 - 0
Redant Drivers/Apex And Drivers/RADataProvider.h

@@ -28,4 +28,8 @@
 
 + (NSDictionary *)reportCurrentLocation:(NSString *)location;
 
++ (NSDictionary *)bindNitificationToken:(NSString *)token;
+
++ (NSDictionary *)logout;
+
 @end

+ 23 - 0
Redant Drivers/Apex And Drivers/RADataProvider.m

@@ -280,6 +280,29 @@
     return [self handleJsonData:json];
 }
 
++ (NSDictionary *)bindNitificationToken:(NSString *)token {
+    
+    if (token.length == 0) {
+        return @{@"result" : @(0)};
+    }
+    
+    NSMutableDictionary *params = [NSMutableDictionary dictionary];
+    [params setObject:token forKey:@"token"];
+    
+    NSData* json=[self get_json:URL_HOST parameters:params  file:nil];
+    
+    return [self handleJsonData:json];
+}
+
++ (NSDictionary *)logout {
+    
+    NSMutableDictionary *params = [NSMutableDictionary dictionary];
+    
+    NSData* json=[self get_json:URL_HOST parameters:params  file:nil];
+    
+    return [self handleJsonData:json];
+}
+
 + (NSDictionary *)loadFakeData:(NSString *)fileName {
     
     NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];

+ 1 - 0
Redant Drivers/Apex And Drivers/RANotificationNameCenter.h

@@ -12,3 +12,4 @@
 FOUNDATION_EXTERN NSString * const RANotificationNewOrder;
 FOUNDATION_EXTERN NSString * const RANotificationReloadHome;
 FOUNDATION_EXTERN NSString * const RANotificationLogout;
+FOUNDATION_EXTERN NSString * const RANotificationGoDetail;

+ 1 - 0
Redant Drivers/Apex And Drivers/RANotificationNameCenter.m

@@ -12,3 +12,4 @@
 NSString * const RANotificationNewOrder     = @"RANotificationNewOrder";
 NSString * const RANotificationReloadHome   = @"RANotificationReloadHome";
 NSString * const RANotificationLogout       = @"RANotificationLogout";
+NSString * const RANotificationGoDetail     = @"RANotificationGoDetail";

+ 10 - 2
Redant Drivers/Apex And Drivers/RASingleton.h

@@ -13,18 +13,26 @@
 
 + (instancetype)sharedInstance;
 
-@property (nonatomic,copy) NSString *user;
-@property (nonatomic,copy) NSString *password;
+@property (nonatomic,copy,readonly) NSString *user;
+@property (nonatomic,copy,readonly) NSString *password;
 @property (nonatomic,strong) CLLocation *currentLocation;
 
 @property (nonatomic,copy,readonly) NSString *secretKey;
 @property (nonatomic,copy,readonly) NSString *encryptUser;
 @property (nonatomic,copy,readonly) NSString *encryptPassword;
 
+@property (nonatomic,copy) NSString *notificationToken;
+
 - (void)saveUserInfo;
 
 - (NSString *)savedUser;
 
 - (NSString *)savedPassword;
 
+- (void)loginUser:(NSString *)user password:(NSString *)password;
+
+- (BOOL)autoLogin;
+
+- (void)logout;
+
 @end

+ 41 - 0
Redant Drivers/Apex And Drivers/RASingleton.m

@@ -68,4 +68,45 @@ static RASingleton *singleton;
     return password;
 }
 
+- (void)loginUser:(NSString *)user password:(NSString *)password {
+    _user = user;
+    _password = password;
+    
+    [self saveUserInfo];
+    
+    [self bindUserWithNotificationToken];
+}
+
+- (BOOL)autoLogin {
+    _user = [self savedUser];
+    _password = [self savedPassword];
+    
+    return _user.length && _password.length;
+}
+
+- (void)setNotificationToken:(NSString *)notificationToken {
+    _notificationToken = notificationToken;
+    
+    [self bindUserWithNotificationToken];
+}
+
+- (void)bindUserWithNotificationToken {
+    
+    if (_user.length && _password.length && _notificationToken.length) {
+        [RADataProvider bindNitificationToken:self.notificationToken];
+    }
+}
+
+- (void)logout {
+    _user = nil;
+    _password = nil;
+    
+    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
+    [defaults removeObjectForKey:@"user"];
+    [defaults removeObjectForKey:@"password"];
+    [defaults synchronize];
+}
+
+
+
 @end