Question

Similar questions appear several times on StackOverflow with no solution for Amazon SNS.

This documentation states that registering devices for Amazon SNS (APNS) can be completed through an Anonymous Token Vending Machine. This documentation provides a template for deployment, which I have used, then this code sample specifies an iOS client which demonstrates its use. However, not in the template configuration source or the iOS client example do I see how the TVM is linked to the SNS application to create device entries (referred to as endpoints). Any ideas where this occurs?

Was it helpful?

Solution

Turns out reference is linked client-side. The solution was as follows:

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top