2017-01-12 7 views
0

Urban Airship 웹 사이트에서 푸시 알림을받을 수 있지만 앱에서 푸시 알림을받을 수있는 방법은 무엇입니까?Urban Airship을 사용하여 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

내가이 내가 NSLog에서 무엇을 얻을 당신이 -(void)sendPush.

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

에있는 NSURLConnection 당신의 을 시작하는 것을 잊었다 가정 :'[D] __50- [UAEventManager enqueueUploadOperationWithDelay :] _ block_invoke_3 [라인 (372)] 분석 업로드 성공 : {URL : https://combine.urbanairship.com/warp9/} {상태 코드 : 200, 헤더 { Connection = "keep-alive"; "Content-Length"= 0; "Content-Type"= "application/json"; 날짜 = "목요일, 201 1 월 12 일 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 일괄 처리 간격"= 60; }}' – Hugo