If you want to link a terminal to topic to SNS, there is an example of directly accessing SNS from a smartphone terminal using sdk, but considering the future (such as managing tokens on the dynamoDB side as well), the server When I tried to do it on the side side, there were surprisingly few cases, so I summarized it.
This time, the push notification target is a smartphone, so that is the premise. Also, I'm implementing it in Lambda, but I don't think it depends on Lambda.
handler.py
import boto3
def lambda_handler(event, context):
application_arn = 'arn:aws:sns:us-east-1:999999999999:notify_sample'
topic_arn = 'arn:aws:sns:us-east-1:999999999999:notify_sample'
endpoint = add_endpoint(application_arn, token)
subscribe_token(topic_arn, endpoint['EndpointArn'], is_subscribe)
The rough procedure is
It will be. Please register and acquire the ARN of Application and Topic appearing here using the dashboard on the AWS side in advance.
handler.py
def add_endpoint(application_arn, token):
# token(boto3 doc excerpt) :
# For example, when using APNS as the notification service, you need the device token.
# Alternatively, when using GCM or ADM, the device token equivalent is called the registration ID.
client = boto3.client('sns')
endpoint = client.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token=token
)
return endpoint
Call it even if it has already been added. token is a token obtained from APNS or FCM (GCM). create_platform_endpoint () returns the information of the existing endpoint without adding a new one if the endpoint has already been added. In any case, endpoint information can be obtained, so subscribe based on this information.
The CreatePlatformEndpoint action is idempotent, so if the requester already owns an endpoint with the same device token and attributes, that endpoint's ARN is returned without creating a new endpoint.
This is a bit of a hassle.
You can simply execute subscribe () to subscribe, but in the case of unsubscribe, SubscriptionArn of the target terminal is required. This time, I execute subscribe () once to get SubscriptionArn, and then use it to execute unsubscribe ().
handler.py
def subscribe_token(topic_arn, endpoint_arn, is_subscribe):
client = boto3.client('sns')
subscription = client.subscribe(
TopicArn=topic_arn,
Protocol='application',
Endpoint=endpoint_arn
)
if not is_subscribe:
client.unsubscribe(
SubscriptionArn=subscription['SubscriptionArn']
)
This is the last method to get Subscription Arn, but even if you look at the manual, you can only find such a good method. Please point out if there is an appropriate method.
I got a slightly unclear error at runtime, so I reviewed the boto3 manual and reviewed the code, but I think that it is more straightforward to write as follows. However, rewriting did not improve the error, so I think the client is a low-level client.
handler.py
def add_endpoint(dry_run, application_arn, token):
sns = boto3.resource('sns')
platform_application = sns.PlatformApplication(application_arn)
endpoint = platform_application.create_platform_endpoint(
Token=token
)
return endpoint.arn
def subscribe_token(dry_run, topic_arn, endpoint_arn, is_subscribe):
sns = boto3.resource('sns')
topic = sns.Topic(topic_arn)
subscription = topic.subscribe(
TopicArn=topic_arn,
Protocol='application',
Endpoint=endpoint_arn
)
if not is_subscribe:
subscription.delete()
Recommended Posts