2017-01-12 1 views
0

Я получаю push-уведомления на устройстве iOS при отправке их с веб-сайта Urban Airship, но не могу понять, как отправить их из приложения ...Отправить Push-уведомление от iOS, используя городской дирижабль

Это код, который я использую для отправки уведомлений из приложения:

- (void)sendPush { 

    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/vnd.urbanairship+json; version=3;" forHTTPHeaderField:@"Accept"]; 

    NSDictionary * push = @{ 

          @"audience" : @"all", 

          @"device_types" : @[@"ios"], 

          @"notification" : @{ 

            @"ios": @{ 

            @"alert" : @"ALERT", 
            @"sound" : @"default", 
            @"badge" : @"auto", 

            } 
          }, 

          @"message": @{ 

            @"title": @"TITLE", 
            @"body": @"BODY", 
            @"content_type": @"text/html" 

            } 
          }; 

    NSData *pushdata = [NSJSONSerialization dataWithJSONObject:push options:0 error:NULL]; 

    [request setHTTPBody:pushdata]; 

    [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]) { 

     NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:[UAConfig defaultConfig].developmentAppKey password:[UAConfig defaultConfig].developmentAppSecret persistence:NSURLCredentialPersistenceForSession]; 

     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    NSHTTPURLResponse * res = (NSHTTPURLResponse *) response; 

    NSLog(@"response: %@",res); 
    NSLog(@"res %li\n",(long)res.statusCode); 

    if (res.statusCode == 202) { 

     NSLog(@".success"); 
    } 
} 

AppDelegate.m. -didFinishLaunchingWithOptions:

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { 

    UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 

} else { 

    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 

    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; 
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError *error) { 

    }]; 

    [UNUserNotificationCenter currentNotificationCenter].delegate = (id)self;  

    #endif 
} 

[[UIApplication sharedApplication] registerForRemoteNotifications]; 

UAConfig *config = [UAConfig defaultConfig]; 

[UAirship takeOff:config]; 

[UAirship push].userPushNotificationsEnabled = YES; 

Что не так с этим кодом?

ответ

0

Я полагаю, вы забыли, чтобы начать свой NSURLConnection в -(void)sendPush.

[[NSURLConnection connectionWithRequest:request delegate:self] start]; 
+0

Это то, что я получаю в NSLog: '[D] __50- [UAEventManager enqueueUploadOperationWithDelay:] _ block_invoke_3 [Line 372] Аналитическое успех загрузки: {URL: https://combine.urbanairship.com/warp9/} {код состояния: 200, заголовки { Connection = "keep-alive"; «Content-Length» = 0; "Content-Type" = "application/json"; Дата = "Чт, 12 янв. 2017 19:27:40 GMT"; Сервер = "nginx/1.6.2"; Vary = "Accept-Encoding"; «X-UA-Max-Batch» = 500; «X-UA-Max-Total» = 5120; "X-UA-Max-Wait" = 604800; «X-UA-Min-Batch-Interval» = 60; }} ' – Hugo

Смежные вопросы