Send APNS Push Messages with Boto3
The tricky thing about sending APNS push messages via Boto3 is that you have to make sure you encode the json twice. Once for the actual payload and then for the message that’s published. In order for it to work though the MessageStructure
argument must be set to 'json'
. Here’s a mini example.
Note(s):
- APNS Payload format examples can be found here.
- You definitely need an AWS account.
- In Boto3, if you have your access key, secret key, and region name in environment variables, you don’t have to set them as arguments when initializing a resource.
from boto3 import resource
import json
res = resource(
'sns',
aws_access_key_id=Y0urAWSACCESSKEY,
aws_secret_access_key=Y0uRAWSSECRETKEY,
region_name='us-east-2')
endpoint = res.PlatformEndpoint("L3g1tRegist3redDev1ceARN::Bruh")
txt = "I sent this via Boto3!"
msg_dict = {
"aps": {
"alert": txt
}
}
payload = {"APNS": json.dumps(msg_dict)}
endpoint.publish(
Message=json.dumps(payload),
MessageStructure='json'
)