通过亚马逊匿名TVM注册iOS设备以与Amazon SNS(APNS)配合使用

时间:2014-04-21 21:41:32

标签: ios amazon-web-services amazon-sns

类似的问题在StackOverflow上多次出现,没有针对Amazon SNS的解决方案。

This documentation指出,为Amazon SNS(APNS)注册设备可以通过匿名令牌自动售货机完成。 This documentation提供了我用过的部署模板,然后this code sample指定了一个演示其用途的iOS客户端。但是,不是在模板配置源或iOS客户端示例中,我是否看到TVM如何链接到SNS应用程序以创建设备条目(称为端点)。有这种想法的想法吗?

1 个答案:

答案 0 :(得分:2)

原来参考链接客户端。解决方案如下:

Include:

AWSRuntime.framework
AWSSecurityTokenService.framework
AWSSNS.framework


#define SNS_PLATFORM_APPLICATION_ARN @"Insert ARN here"
#define TOKEN_VENDING_MACHINE_URL @"Insert vending machine url, found in output tab"
#define USE_SSL 1

#pragma mark - Amazon TVM


+ (void)initSNS
{
    AmazonCredentials *credentials = [AmazonKeyChainWrapper getCredentialsFromKeyChain];

    sns = [[AmazonSNSClient alloc] initWithCredentials:credentials];
    sns.endpoint = [AmazonEndpoints snsEndpoint:US_WEST_2];

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];
}

+ (AmazonTVMClient *)tvm
{
    if (tvm == nil) {
        tvm = [[AmazonTVMClient alloc] initWithEndpoint:TOKEN_VENDING_MACHINE_URL useSSL:USE_SSL];
    }

    return tvm;
}

+ (bool)hasCredentials
{
    return ![TOKEN_VENDING_MACHINE_URL isEqualToString:@"CHANGE ME"];
}

+ (Response *)validateCredentials
{
    Response *ableToGetToken = [[Response alloc] initWithCode:200 andMessage:@"OK"];

    if ([AmazonKeyChainWrapper areCredentialsExpired]) {

        @synchronized(self)
        {
            if ([AmazonKeyChainWrapper areCredentialsExpired]) {

                ableToGetToken = [[self tvm] anonymousRegister];

                if ( [ableToGetToken wasSuccessful])
                {
                    ableToGetToken = [[self tvm] getToken];

                    if ( [ableToGetToken wasSuccessful])
                    {
                        [AppDelegate initSNS];
                    }
                }
            }
        }
    }
    else
    {
        [AppDelegate initSNS];
    }

    return ableToGetToken;
}

#pragma mark - Push Notification

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    application.applicationIconBadgeNumber = 0;
    NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
    NSLog( @"%@", msg );

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:[NSString stringWithFormat:@"%@", msg]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}


-(NSString*)deviceTokenAsString:(NSData*)deviceTokenData
{
    NSString *rawDeviceTring = [NSString stringWithFormat:@"%@", deviceTokenData];
    NSString *noSpaces = [rawDeviceTring stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *tmp1 = [noSpaces stringByReplacingOccurrencesOfString:@"<" withString:@""];

    return [tmp1 stringByReplacingOccurrencesOfString:@">" withString:@""];
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog( @"Submit the device token [%@] to SNS to receive notifications.", deviceToken );
    SNSCreatePlatformEndpointRequest *platformEndpointRequest = [SNSCreatePlatformEndpointRequest new];
    platformEndpointRequest.customUserData =  @"Here's the custom data for the user.";
    platformEndpointRequest.token = [self deviceTokenAsString:deviceToken];
    platformEndpointRequest.platformApplicationArn = SNS_PLATFORM_APPLICATION_ARN;

    [sns createPlatformEndpoint:platformEndpointRequest];
}
相关问题