// // AppDelegate.m // Juick // // Created by Vitaly Takmazov on 26.10.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "AppDelegate.h" #import "MessagesViewController.h" #import "LoginViewController.h" #import "ColorScheme.h" #import "APIClient.h" #import "Message.h" #import "User.h" #import "DeviceRegistration.h" #import "NewPostViewController.h" #import "DiscoverViewController.h" @interface AppDelegate() -(void) parseNotificationPayload:(NSDictionary *)userInfo; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UINavigationBar appearance] setTintColor:[ColorScheme linkColor]]; //[[UINavigationBar appearance] setBarTintColor:[ColorScheme headerBackground]]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [ColorScheme linkColor]}]; [[UIToolbar appearance] setTintColor:[ColorScheme linkColor]]; [[UITabBar appearance] setTintColor:[ColorScheme linkColor]]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; if ([[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"] == nil) { [[PDKeychainBindings sharedKeychainBindings] setObject:nil forKey:@"com.juick.username"]; [[PDKeychainBindings sharedKeychainBindings] setObject:nil forKey:@"com.juick.password"]; [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"FirstRun"]; } [self registerForRemoteNotifications]; NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; [self parseNotificationPayload:userInfo]; return YES; } - (void)registerForRemoteNotifications { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ if(!error){ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }]; } }]; } -(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; DeviceRegistration *registration = [DeviceRegistration new]; registration.type = @"apns"; registration.token = token; AFHTTPSessionManager *manager = [APIClient sharedClient].manager; [manager PUT:@"/notifications" parameters:[@[registration] yy_modelToJSONObject] success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"success"); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"fail %@", [error localizedDescription]); } ]; } -(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Error: %@", [error debugDescription]); } //Called when a notification is delivered to a foreground app. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); } //Called to let your app know which action was selected by the user for a given notification. -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{ NSDictionary *userInfo = response.notification.request.content.userInfo; [self parseNotificationPayload:userInfo]; UITabBarController *main = (UITabBarController *)self.window.rootViewController; if ([self.pushedThread integerValue] > 0) { [main setSelectedIndex:0]; UINavigationController *discover = (UINavigationController *)[main.viewControllers objectAtIndex:0]; [[discover.viewControllers objectAtIndex:0] performSegueWithIdentifier:@"threadViewSegue" sender:discover]; } else { [main setSelectedIndex:1]; UINavigationController *dialogs = (UINavigationController *)[main.viewControllers objectAtIndex:1]; [[dialogs.viewControllers objectAtIndex:0] performSegueWithIdentifier:@"chatSegue" sender:dialogs]; } completionHandler(); } -(void) parseNotificationPayload:(NSDictionary *)userInfo { if (userInfo != nil) { self.pushedThread = [userInfo objectForKey:@"mid"]; self.pushedUname = [userInfo objectForKey:@"uname"]; } } @end