Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gjedeer
Created June 24, 2015 22:01
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gjedeer/45c0ca4b2fdc471d6a48 to your computer and use it in GitHub Desktop.
Save gjedeer/45c0ca4b2fdc471d6a48 to your computer and use it in GitHub Desktop.
How to send a push notification directly to device using Python, Boto and SNS
import boto
import boto.exception
import boto.sns
import pprint
import re
def send_push(device_id, body):
region = [r for r in boto.sns.regions() if r.name==u'eu-west-1'][0]
sns = boto.sns.SNSConnection(
aws_access_key_id="YOUR_AWS_ACCESS_KEY_HERE",
aws_secret_access_key="YOUR_AWS_ACCESS_SECRET_HERE",
region=region,
)
try:
endpoint_response = sns.create_platform_endpoint(
platform_application_arn='arn:aws:sns:eu-west-1:123456879:app/APNS_SANDBOX/Myapp_Dev',
token=device_id,
)
endpoint_arn = endpoint_response['CreatePlatformEndpointResponse']['CreatePlatformEndpointResult']['EndpointArn']
except boto.exception.BotoServerError, err:
# Yes, this is actually the official way:
# http://stackoverflow.com/questions/22227262/aws-boto-sns-get-endpoint-arn-by-device-token
result_re = re.compile(r'Endpoint(.*)already', re.IGNORECASE)
result = result_re.search(err.message)
if result:
endpoint_arn = result.group(0).replace('Endpoint ','').replace(' already','')
else:
raise
print "ARN:", endpoint_arn
publish_result = sns.publish(
target_arn=endpoint_arn,
message=body,
)
print "PUBLISH"
pprint.pprint(publish_result)
@Mahmoud-Alaa-Eldeen
Copy link

Mahmoud-Alaa-Eldeen commented Nov 4, 2017

sunilpotu, you can publish a msg to a topic here https://gist.github.com/kylefritz/633175 .

gjedeer, please tell me the required boto libs that are needed for above code, as adding all boto files increased my project directory
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment