// // 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; [self registerForRemoteNotifications]; NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo) { [self parseNotificationPayload:userInfo]; } else { if ([[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"] == nil) { [SAMKeychain deletePasswordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"]; [SAMKeychain deletePasswordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"]; [SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlock]; [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"FirstRun"]; } } 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:@""]; [[NSOperationQueue new] addOperationWithBlock:^{ DeviceRegistration *registration = [DeviceRegistration new]; registration.type = @"apns"; registration.token = token; [[APIClient sharedClient] refreshDeviceRegistration:registration callback:^(BOOL success) { if (success) { NSLog(@"successfully refreshed registration with %@", token); } }]; }]; } -(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]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ UITabBarController *main = (UITabBarController *)self.window.rootViewController; if ([self.pushedThread integerValue] > 0) { [main setSelectedIndex:0]; UINavigationController *discover = (UINavigationController *)[main.viewControllers objectAtIndex:0]; [discover popToRootViewControllerAnimated:NO]; [[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)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if (userInfo[@"service"]) { application.applicationIconBadgeNumber = [userInfo[@"user"][@"unreadCount"] integerValue]; [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray * _Nonnull notifications) { for (UNNotification* notification in notifications) { if ([notification.request.content.userInfo[@"mid"] integerValue] == [userInfo[@"mid"] integerValue]) { [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]]; } } }]; } completionHandler(UIBackgroundFetchResultNewData); } -(void) parseNotificationPayload:(NSDictionary *)userInfo { self.pushedThread = userInfo[@"mid"]; self.pushedUname = userInfo[@"uname"]; } +(AppDelegate *) shared { return (AppDelegate *)[UIApplication sharedApplication].delegate; } @end