2014-08-31 32 views
2

當我在SNS控制檯中設置端點時,我有推送工作。現在我正在轉向允許iOS應用程序創建端點。然後訂閱。iOS中的AWS v2 SNS - createPlatformEndpoint:請求

問題是BFTask *task = [sns createPlatformEndpoint:request];返回,task.result是零,而不是AWSSNSCreateEndpointResponse*。順便說一句,task.error也是零。

從.h文件:

  • @return BFTask的一個實例。在成功執行時,task.result將包含AWSSNSCreateEndpointResponse的一個實例。在執行失敗時,task.error可能包含帶有AWSSNSErrorDomain domian的NSError和以下錯誤代碼:AWSSNSErrorInvalidParameter,AWSSNSErrorInternalError,AWSSNSErrorAuthorizationError,AWSSNSErrorNotFound。

亞馬遜已經爲SDK v2中的SNS創建了網絡示例。所以我也在尋找可能有一些使用SDK v2創建端點和訂閱的例子。在應用程序啓動

後,我打電話給我的方法來自:

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

代碼:

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

這裏是任務描述:

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

下面是詳細的日誌輸出:

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] 

,沒有錯誤的回報,我在什麼地方出了錯......想法虧損?

**更新使用異步完成塊**

// 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; 
    }]; 
} 
+0

有沒有什麼辦法可以得到完整的代碼片段。現在aws sns在禁用端點時會殺了我 – 2016-04-12 06:34:45

+0

當然 - 哪個部分?創建終點,主題創建和/或訂閱該主題的終點? – Kent 2016-04-12 14:35:22

回答

4

createPlatformEndpoint:是一種異步方法。您需要撥打continueWithBlock:BFTask的實例以檢索task.resulttask.error。這blog post應該可以幫助你瞭解如何使用BFTask。

+0

博客文章看起來不錯。我將在今晚實施該模塊並進行測試。讓你知道!謝謝。 – Kent 2014-08-31 16:41:31

+0

完美,我現在得到一個合理的錯誤信息。謝謝。 – Kent 2014-09-01 15:25:55

+0

修正了權限錯誤,現在正在創建端點。 – Kent 2014-09-01 17:14:07