sns自动将端点注册到主题

时间:2014-12-09 20:12:23

标签: android ios push-notification amazon-sns

我使用phonegap为ios和android构建了一个应用程序。我正在使用sns api创建端点并为aws中的不同Apps注册令牌。我需要APP的端点来订阅主题,因此我们可以向ios和android发送一般消息。有没有办法在sns仪表板中自动执行此操作,还是需要使用api执行此操作?

2 个答案:

答案 0 :(得分:1)

我正在处理类似的问题,based on the documentation您必须在收到GCM或APNS的推送通知凭据时使用API​​订阅每个客户端。您可以通过CSV文件或使用bulkupload API批量上传令牌来启动系统(请参阅"使用CreatePlatformEndpoint API上传多个令牌"以link为例)。

您可能也不希望在您的移动应用程序中嵌入AWS凭据,因此拥有某种平台后端,其中移动应用程序提交其推送凭据(实质上是代理),并且这些凭据将转发到AWS SNS以获取各种主题一个好主意。

答案 1 :(得分:0)

这就是我所做的。要使用此代码,您需要创建IAM凭证。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    ...
        //Push Notification register
        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
        [application registerForRemoteNotifications];

        // Sets up the AWS Mobile SDK for iOS
        // Initialize the Amazon credentials provider
        AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:AWSAccessID secretKey:AWSSecretKey];

        AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
                                                                             credentialsProvider:credentialsProvider];

        [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

...
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{       
        NSLog(@"My token is: %@", deviceToken);
        NSString *deviceTokenString = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

        [[NSUserDefaults standardUserDefaults] setObject:deviceTokenString forKey:@"deviceToken"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        AWSSNS *sns = [AWSSNS defaultSNS];
        AWSSNSCreatePlatformEndpointInput *request = [AWSSNSCreatePlatformEndpointInput new];
        request.token = deviceTokenString;
        request.platformApplicationArn = SNSPlatformApplicationArn;
        [[[sns createPlatformEndpoint:request] continueWithBlock:^id(BFTask *task)
        {
            AWSSNSSubscribeInput *subReq = [AWSSNSSubscribeInput new];
            if (task.error != nil)
            {
                NSLog(@"Error: %@",task.error);
            }
            else
            {
                AWSSNSCreateEndpointResponse *createEndPointResponse = task.result;
                NSLog(@"endpointArn: %@",createEndPointResponse);
                [[NSUserDefaults standardUserDefaults] setObject:createEndPointResponse.endpointArn forKey:@"endpointArn"];
                [[NSUserDefaults standardUserDefaults] synchronize];

                //subscribe to topic
                subReq.endpoint = createEndPointResponse.endpointArn;
                subReq.topicArn = SNSTopicArn;
                subReq.protocols = @"application";
            }
            return [sns subscribe:subReq];
        }] continueWithBlock:^id(BFTask *task)
        {
            if (task.cancelled)
                NSLog(@"Task Cancelled");
            else if (task.error)
                NSLog(@"Error: [%@]", task.error);
            else
                NSLog(@"Success");

            return nil;
        }];
}