使用Urban Airship从iOS发送推送通知

时间:2017-01-12 15:36:43

标签: ios objective-c push-notification urbanairship.com

我可以在Urban Airship网站上发送推送通知,但无法弄清楚如何从应用中发送推送通知......

这是我用来从应用发送通知的代码:

- (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;

此代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我想您忘了在-(void)sendPush.

中启动 NSURLConnection
[[NSURLConnection connectionWithRequest:request delegate:self] start];