summaryrefslogtreecommitdiff
path: root/Juick/AppDelegate.m
blob: 202ac821cff8e353729416ad359d80c615a48a4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
//  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]];
    
    [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;
    NSLog(@"User Info : %@", userInfo);
    [self parseNotificationPayload:userInfo];
    UITabBarController *main = (UITabBarController *)self.window.rootViewController;
    [main setSelectedIndex:0];
    UINavigationController *discover = (UINavigationController *)[main.viewControllers objectAtIndex:0];
    [discover performSegueWithIdentifier:@"threadViewSegue" sender:discover];
    completionHandler();
}

-(void) parseNotificationPayload:(NSDictionary *)userInfo {
    if (userInfo != nil) {
        self.pushedThread = [userInfo objectForKey:@"mid"];
    }
}

@end