Skip to content

Instantly share code, notes, and snippets.

@namit
Last active August 29, 2015 14:00
Show Gist options
  • Save namit/11189637 to your computer and use it in GitHub Desktop.
Save namit/11189637 to your computer and use it in GitHub Desktop.
Publishing messages in Amazon Simple Notification Servicce (SNS) + Apple Apple Push Notification service (APNS) + Ruby
def self.sendapns(apnsdata)
type = apnsdata['type']
uid = apnsdata['userid'].to_i
endpoint_arn = get_endpoint_arn(uid)
notification = APNS_NOTIFICATIONS[type] # Get the notification fields from wherever you store them
if notification.nil?
@log.error("notification type '#{type}' not found")
return
end
aws_message = {
default: notification['body'],
APNS: {
aps: {
alert: { body: notification['body'], 'action-loc-key' => notification['action-loc-key'] },
sound: notification['sound'],
badge: notification['badge']
},
custom: notification['custom']
}.to_json,
APNS_SANDBOX: {
aps: {
alert: { body: notification['body'], 'action-loc-key' => notification['action-loc-key'] },
sound: notification['sound'],
badge: notification['badge']
},
custom: notification['custom']
}.to_json
}
begin
sns = AWS::SNS.new(:access_key_id => AWSPUB, :secret_access_key => AWSPRI)
sns.client.publish(:message => aws_message.to_json, :target_arn => endpoint_arn, :message_structure => 'json')
rescue => err
@log.error("Failed to send #{apnsdata.inspect} with error: #{err.message}\nBacktrace: #{err.backtrace}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment