AWS v2 SNS on iOS - createPlatformEndpoint: request
I have a push running when I set the endpoint in the SNS console. I now move on to creating an iOS app to create an endpoint. Then subscriptions.
The problem is that it BFTask *task = [sns createPlatformEndpoint:request];
returns, task.result is zero, not AWSSNSCreateEndpointResponse*
. BTW, task.error is also null.
From the .h file:
- @return An instance of BFTask. If successful, task.result will contain an instance of AWSSNSCreateEndpointResponse. If it fails, task.error may contain an NSError with domian AWSSNSErrorDomain and the following error code: AWSSNSErrorInvalidParameter, AWSSNSErrorInternalError, AWSSNSErrorAuthorizationError, AWSSNSErrorNotFound.
Amazon has not yet created examples for SNS in SDK v2. So I am also looking for someone who can have examples of creating endpoints and subscribing with SDK v2.
After starting the application, I call my method from:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[self awsStartWithDeviceToken:deviceToken];
}
Code:
- (void)awsStartWithDeviceToken:(NSData *)deviceToken {
// Get a hex string for the NSData deviceToken
// http://stackoverflow.com/questions/7520615/how-to-convert-an-nsdata-into-an-nsstring-hex-string
NSUInteger dataLength = [deviceToken length];
NSMutableString *deviceTokenString = [NSMutableString stringWithCapacity:dataLength*2];
const unsigned char *dataBytes = [deviceToken bytes];
for (NSInteger idx = 0; idx < dataLength; ++idx) {
[deviceTokenString appendFormat:@"%02x", dataBytes[idx]];
}
// Set the log level
[AWSLogger defaultLogger].logLevel = AWSLogLevelVerbose;
// Login
AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:AWS_ACCESSKEY secretKey:AWS_SECRETKEY];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWS_REGION credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
// Create SNS Client
//AWSSNS *sns = [[AWSSNS new] initWithConfiguration:configuration];
AWSSNS *sns = [AWSSNS defaultSNS];
// Create an Application Endpoint
AWSSNSCreatePlatformEndpointInput *request = [AWSSNSCreatePlatformEndpointInput new];
request.token = deviceTokenString;
request.platformApplicationArn = AWS_APP_ARN;
request.customUserData = [NSString stringWithFormat:@"Operation:%@ Alias:%@ Name:%@ Model:%@ (%@:%@)",
[self getOperation], [self getNotifyAlias], [UIDevice currentDevice].name, [UIDevice currentDevice].model,
[UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion] ;
BFTask *task = [sns createPlatformEndpoint:request];
}
Here's a description of the task:
- task.result = (id) 0.0
- task.error = (NSError *) nil
Here is the verbose log output:
2014-08-30 19:04:15.346 iFlightBag[4080:2492646] AWSiOSSDKv2 [Verbose] AWSURLRequestSerialization.m line:462 | -[AWSQueryStringRequestSerializer serializeRequest:headers:parameters:] | Request body: [Action=CreatePlatformEndpoint&Token=0b96783052a5114772ddc9154439849bbff9a6ba364cc3a69bf0524576a7697a&Version=2010-03-31&CustomUserData=Operation%3A%20Alias%3AAirCertChartersBI_%20Name%3ANikko%20Model%3AiPad%20%28iPhone%20OS%3A8.0%29&PlatformApplicationArn=arn%3Aaws%3Asns%3Aus-west-2%3A245211809793%3Aapp%2FAPNS_SANDBOX%2FLevelFlightMobile-Dev]
2014-08-30 19:04:15.364 iFlightBag[4080:2492646] AWSiOSSDKv2 [Debug] AWSSignature.m line:307 | -[AWSSignatureV4Signer signRequestV4:] | AWS4 Canonical Request: [POST
/
content-type:application/x-www-form-urlencoded; charset=utf-8
host:sns.us-west-2.amazonaws.com
user-agent:aws-sdk-iOS/2.0.6 iPhone-OS/8.0 en_US
x-amz-date:20140831T020415Z
content-type;host;user-agent;x-amz-date
76b8df9e95aaea3610c4a67073a0cbe48a941e41507cd55462f6bca2e47134b3]
2014-08-30 19:04:15.365 iFlightBag[4080:2492646] AWSiOSSDKv2 [Debug] AWSSignature.m line:308 | -[AWSSignatureV4Signer signRequestV4:] | payload Action=CreatePlatformEndpoint&Token=0b96783052a5114772ddc9154439849bbff9a6ba364cc3a69bf0524576a7697a&Version=2010-03-31&CustomUserData=Operation%3A%20Alias%3AAirCertChartersBI_%20Name%3ANikko%20Model%3AiPad%20%28iPhone%20OS%3A8.0%29&PlatformApplicationArn=arn%3Aaws%3Asns%3Aus-west-2%3A245211809793%3Aapp%2FAPNS_SANDBOX%2FLevelFlightMobile-Dev
2014-08-30 19:04:15.367 iFlightBag[4080:2492646] AWSiOSSDKv2 [Debug] AWSSignature.m line:324 | -[AWSSignatureV4Signer signRequestV4:] | AWS4 String to Sign: [AWS4-HMAC-SHA256
20140831T020415Z
20140831/us-west-2/sns/aws4_request
e60736ba7af965e80d99ccf2cab6298ffddb79de719f317901a0c29ed5cd33c8]
No error returns, I'm at a loss what went wrong ... Ideas?
** Update to use async completion blocks **
// Async call to create the platform endpoint
[[[sns createPlatformEndpoint:request] continueWithSuccessBlock:^id(BFTask *task) {
// success
[self awsCreateTopicsAndSubscriptionsForEndpoint:task.result];
return nil;
}] continueWithBlock:^id(BFTask *task) {
// failed
if (task.error) {
NSLog(@"Error: %@", task.error);
}
return nil;
}];
}
source to share