Featured

2014年4月29日星期二

Setup Amazon SNS Mobile Push for ios App —5

18. Modify end point region in MessageBoard.m :


-(id)init
{
    self = [super init];
    if (self != nil) {
        snsClient = [[AmazonSNSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
        snsClient.endpoint = [AmazonEndpoints snsEndpoint:US_WEST_1];//US_EAST_1

        sqsClient = [[AmazonSQSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
        sqsClient.endpoint = [AmazonEndpoints sqsEndpoint:US_WEST_1];
        
        // Find the Topic for this App or create one.
        topicARN = [self findTopicArn];
        if (topicARN == nil) {
            topicARN = [self createTopic];
        }
        
        // Find the Queue for this App or create one.
        queueUrl = [self findQueueUrl] ;
        if (queueUrl == nil) {
            queueUrl = [self createQueue];
            
            // Allow time for the queue to be created.
            [NSThread sleepForTimeInterval:4.0];
            
            [self subscribeQueue];
        }
        
        // Find endpointARN for this device if there is one.
        endpointARN = [self findEndpointARN] ;
        
    }
    
    return self;
}


19. Modify End Point User Default Setting :


-(bool)createApplicationEndpoint{
    
    NSString *deviceToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"myDeviceToken"];
    if (!deviceToken) {
        [[Constants universalAlertsWithTitle:@"deviceToken not found!" andMessage:@"Device may fail to register with Apple's Notification Service, please check debug window for details"] show];
    }
    
    SNSCreatePlatformEndpointRequest *endpointReq = [[SNSCreatePlatformEndpointRequest alloc] init];
    endpointReq.platformApplicationArn = PLATFORM_APPLICATION_ARN;
    endpointReq.token = deviceToken;
    
    SNSCreatePlatformEndpointResponse *endpointResponse = [snsClient createPlatformEndpoint:endpointReq];
    if (endpointResponse.error != nil) {
        NSLog(@"Error: %@", endpointResponse.error);
        [[Constants universalAlertsWithTitle:@"CreateApplicationEndpoint Error" andMessage:endpointResponse.error.userInfo.description] show];
        return NO;
    }
    
    endpointARN = endpointResponse.endpointArn;
    [[NSUserDefaults standardUserDefaults] setObject:endpointResponse.endpointArn forKey:@"DEVICE_ENDPOINT"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return YES;
}

20. Capture iPhone device push token and create AWS End Point 


 if ([[MessageBoard instance] createApplicationEndpoint]) 
                    NSLog(@" Add end point OK");










沒有留言:

發佈留言