2017-01-12 60 views
0

从Urban Airship网站发送邮件时,我可以在iOS设备上收到推送通知,但无法弄清楚如何从应用程序发送它们...使用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

我想你忘了启动NSURLConnection的-(void)sendPush.

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

这是我得到的NSLog:'[d] __50- [UAEventManager enqueueUploadOperationWithDelay:] _ block_invoke_3 [线路372]分析上传成功: {URL:https://combine.urbanairship.com/warp9/} {状态码:200,标题{0}连接=“保持活动”; “Content-Length”= 0; “Content-Type”=“application/json”; 日期=“星期四,2017年1月12日19时27分40秒”; Server =“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

相关问题