3

I have the push working when I set the endpoint up in the SNS console. Now I am moving to allowing the iOS app to create the endpoint. Then the subscriptions.

The problem is that BFTask *task = [sns createPlatformEndpoint:request]; returns, task.result is nil, not a AWSSNSCreateEndpointResponse*. BTW, task.error is also nil.

From the .h file:

  • @return An instance of BFTask. On successful execution, task.result will contain an instance of AWSSNSCreateEndpointResponse. On failed execution, task.error may contain an NSError with AWSSNSErrorDomain domian and the following error code: AWSSNSErrorInvalidParameter, AWSSNSErrorInternalError, AWSSNSErrorAuthorizationError, AWSSNSErrorNotFound.

Amazon has net yet created examples for SNS in the SDK v2. So I am also looking for someone who may have some examples of endpoint and subscription creation using the SDK v2.

After the app starts, I call my method from:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [self awsStartWithDeviceToken:deviceToken];
} 

The 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 is the task description:

  • task.result = (id) 0.0
  • task.error = (NSError *) nil

Here is the verbose logging 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]

With no error returns, I am at a loss on 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;
    }];
}
Kent
  • 1,374
  • 1
  • 15
  • 29
  • is there any way I can get the full code snippet for this. right now aws sns is killing me when disabling the endpoints – Dani Pralea Apr 12 '16 at 06:34
  • Sure - which part(s)? Creation of the end point, topic creation, and/or subscription of the end point to the topic? – Kent Apr 12 '16 at 14:35

1 Answers1

5

createPlatformEndpoint: is an asynchronous method. You need to call continueWithBlock: on the instance of BFTask in order to retrieve task.result or task.error. This blog post should help you understand how to use BFTask.

Yosuke
  • 3,759
  • 1
  • 11
  • 21