From 2345989ce454342d8ac45077e2cd650d18744066 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 18 Oct 2017 01:48:34 +0300 Subject: device registration --- Juick/AppDelegate.m | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'Juick/AppDelegate.m') diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m index 7be4424..d4f0b8c 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -12,7 +12,7 @@ #import "ColorScheme.h" #import "Message.h" -#import "TokensList.h" +#import "DeviceRegistration.h" #import "NewPostViewController.h" @implementation AppDelegate @@ -25,7 +25,8 @@ [[UIToolbar appearance] setTintColor:[ColorScheme linkColor]]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; - + + [self registerForRemoteNotifications]; return YES; } @@ -34,7 +35,9 @@ center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ if(!error){ - [[UIApplication sharedApplication] registerForRemoteNotifications]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [[UIApplication sharedApplication] registerForRemoteNotifications]; + }]; } }]; } @@ -42,11 +45,15 @@ -(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; - TokensList *tokensList = [TokensList new]; - tokensList.type = @"apns"; - tokensList.tokens = [NSArray arrayWithObjects:token, nil]; + DeviceRegistration *registration = [DeviceRegistration new]; + registration.type = @"apns"; + registration.token = token; AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - [manager PUT:@"/notifications" parameters:[tokensList yy_modelToJSONObject] success:nil failure:nil]; + [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 { -- cgit v1.2.3