Skip to content

Instantly share code, notes, and snippets.

@alexey
Forked from jkotchoff/aws_sns_push_notification.rb
Created February 23, 2017 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexey/f1e1918d13faeb120a0d02b42f1f92ac to your computer and use it in GitHub Desktop.
Save alexey/f1e1918d13faeb120a0d02b42f1f92ac to your computer and use it in GitHub Desktop.
Sends a push notifications to an iOS and an Android device from Amazon SNS V2
require 'rubygems'
require 'aws-sdk'
# This code snippet sends a push notification to a device using the Amazon SNS service.
#
# It is using the preview V2 amazon gem as per:
# https://aws.amazon.com/sdk-for-ruby/
#
# This was installed using:
# $ gem install aws-sdk --pre
#
# Refer to gem doc:
# http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Client.html
#
# SNS was set up in:
# http://console.aws.amazon.com/sns/home
#
# ie. we first set up a GCM, an APNS_SANDBOX and an APNS app in the AWS SNS dashboard.
#
# This guide helped with configuring the iOS apps:
# http://www.adventuresofanentrepreneur.net/creating-a-mobile-appsgames-company/setting-up-aws-sns-to-send-push-notifications-to-ios-devices
#
# Note: In the code below, we are using an IAM user instead of the root Amazon account for this access key and
# secret access key authorization for security purposes.
#
# Refer:
# http://docs.aws.amazon.com/IAM/latest/UserGuide/ManagingCredentials.html
# http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html
#
# ie.
# 1/ Navigate to:
# https://console.aws.amazon.com/iam/home
#
# 2/ Go to 'Groups' and create a new group and select 'Amazon SNS Full Access'
#
# 3/ Go to 'Users' and create a user and associate the group you created
#
# 4/ Click 'Create Access Key' and 'Download Credentials'. Secret key will not be available again so make sure to make a copy of it.
#
# Access key ID example: AKIAIOSFODNN7EXAMPLE
# Secret access key example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
#
# Region must matches the APNS app that was set up in the AWS dashboard
region_name = "us-west-2"
sns = Aws::SNS::Client.new(
region: region_name,
access_key_id: '...',
secret_access_key: '...'
)
# APNS app created in the AWS dashboard
send_ios_message = true
if send_ios_message
# Amazon ARN endpoint for the iOS device being pushed to
ios_application_arn = "arn:aws:sns:us-west-2:12345:app/APNS/appname-ios-production"
jasons_iphone_push_token = "..."
jasons_iphone_endpoint = sns.create_platform_endpoint(
platform_application_arn: ios_application_arn,
token: jasons_iphone_push_token,
custom_user_data: '1234'
).endpoint_arn
# Define a push notification message with metadata
payload = {
notification_id: "15",
aps: {
alert: "Hello World from Ruby",
badge: 4,
sound: "default"
}
}
message = {
APNS: payload.to_json,
APNS_SANDBOX: payload.to_json
}.to_json
resp = sns.publish(
target_arn: jasons_iphone_endpoint,
message: message,
message_structure: "json",
)
end
# GCM app created in the AWS dashboard
send_gcm_message = true
if send_gcm_message
# Amazon ARN endpoint for the Android device being pushed to
android_application_arn = "arn:aws:sns:us-west-2:12345:app/GCM/appname-android"
# Tested using Genymotion with Google Play services as per:
# http://stackoverflow.com/questions/20121883/how-to-install-google-play-services-in-a-genymotion-vm-with-no-drag-and-drop-su
genymotion_push_token = "..."
genymotion_endpoint = sns.create_platform_endpoint(
platform_application_arn: android_application_arn,
token: genymotion_push_token,
custom_user_data: '1235'
).endpoint_arn
time_to_live_seconds = 60 * 60 * 24 * 7
message = {
GCM: {
data: {
message: "Hello World from Ruby AWS",
notification_id: "15",
collapse_key: "Ruby AWS",
time_to_live: time_to_live_seconds
}
}.to_json
}.to_json
resp = sns.publish(
target_arn: genymotion_endpoint,
message: message,
message_structure: "json",
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment