当前,如果我们要使用以下方法将设备添加到SNS应用程序中:
ep = SNSConnection.create_platform_endpoint(app_arn,device_token,user_data)
有一个选项是该设备已在过去添加。
为了验证设备是否已经添加,我们正在使用:
def is_device_registered(device_token):
list_of_endpoints = SNSConnection.list_endpoints_by_platform_application(AC.INPLAY_CHAT_APPLICATION_SNS_ARN)
all_app_endpoints = list_of_endpoints['ListEndpointsByPlatformApplicationResponse']['ListEndpointsByPlatformApplicationResult']['Endpoints']
for ep in all_app_endpoints:
ep_device_token = ep['Attributes']['Token']
if device_token == ep_device_token:
endpoint_arn = ep['EndpointArn']
print 'Found an endpoint for device_token: %s, entry:%s' % (device_token,endpoint_arn)
return endpoint_arn
return None
这是非常低效的,无法扩展。
是否有一个boto sns函数来获取device_token并返回endpoint_arn(如果存在)?
(如果没有,则没有)。
最佳答案
Amazon为您提供有关错误消息的信息。您可以从那里解析它。不是最佳选择,但是可以节省一些数据库查找。
错误:SNS错误-无法将用户预订为SNSInvalidParameter:无效参数: token 原因:端点arn:aws:sns:us-east- [ARN key 的其余部分]已经存在,具有相同的 token ,但属性不同。这是准备使用正则表达式的一些咖啡
if err?
log.error "SNS ERROR - Could not subcribe user to SNS" + err
#Try to get arn from error
result = err.message.match(/Endpoint(.*)already/)
if result?.length
#Assign and remove leading and trailing white spaces.
result = result[1].replace /^\s+|\s+$/g, ""
log.debug "found existing arn-> #{result} "
关于amazon-web-services - aws boto sns-通过设备 token 获取endpoint_arn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227262/