Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alerts can be hashes as well #44

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 41 additions & 20 deletions lib/apn_on_rails/app/models/apn/notification.rb
@@ -1,57 +1,78 @@
# Represents the message you wish to send.
# encoding: utf-8

# Represents the message you wish to send.
# An APN::Notification belongs to an APN::Device.
#
#
# Example:
# apn = APN::Notification.new
# apn.badge = 5
# apn.sound = 'my_sound.aiff'
# apn.alert = 'Hello!'
# apn.device = APN::Device.find(1)
# apn.save
#
#
# To deliver call the following method:
# APN::Notification.send_notifications
#
#
# As each APN::Notification is sent the <tt>sent_at</tt> column will be timestamped,
# so as to not be sent again.
class APN::Notification < APN::Base
include ::ActionView::Helpers::TextHelper
extend ::ActionView::Helpers::TextHelper
serialize :custom_properties

serialize :alert

belongs_to :device, :class_name => 'APN::Device'
has_one :app, :class_name => 'APN::App', :through => :device

# Stores the text alert message you want to send to the device.
#
#
# If the message is over 150 characters long it will get truncated
# to 150 characters with a <tt>...</tt>
def alert=(message)
if !message.blank? && message.size > 150
message = truncate(message, :length => 150)
unless message.blank?
message = message.force_encoding("UTF-8")
message = "#{message.byteslice(0, 147)}..." if message.bytesize > 150
end
write_attribute('alert', message)
end

# Creates a Hash that will be the payload of an APN.
#
#
# Example:
# apn = APN::Notification.new
# apn.badge = 5
# apn.sound = 'my_sound.aiff'
# apn.alert = 'Hello!'
# apn.apple_hash # => {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"}}
#
# Example 2:
# Example 2:
# apn = APN::Notification.new
# apn.badge = 0
# apn.sound = true
# apn.custom_properties = {"typ" => 1}
# apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => "1.aiff"}, "typ" => "1"}
#
# Example 3:
# apn = APN::Notification.new
# apn.badge = 0
# apn.sound = true
# apn.custom_properties = {"typ" => 1}
# apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => "1.aiff", "alert" => {"body" => "test", "action-loc-key" => "test-loc-key"}}, "typ" => "1"}

def apple_hash
result = {}
result['aps'] = {}
result['aps']['alert'] = self.alert if self.alert
if self.alert
if self.alert.kind_of?(Hash)
result['aps']['alert'] = {}
self.alert.each do |key,value|
result['aps']['alert']["#{key}"] = "#{value}"
end
else
result['aps']['alert'] = self.alert
end
end
result['aps']['badge'] = self.badge.to_i if self.badge
if self.sound
result['aps']['sound'] = self.sound if self.sound.is_a? String
Expand All @@ -64,9 +85,9 @@ def apple_hash
end
result
end

# Creates the JSON string required for an APN message.
#
#
# Example:
# apn = APN::Notification.new
# apn.badge = 5
Expand All @@ -76,18 +97,18 @@ def apple_hash
def to_apple_json
self.apple_hash.to_json
end

# Creates the binary message needed to send to Apple.
def message_for_sending
json = self.to_apple_json
message = "\0\0 #{self.device.to_hexa}\0#{json.length.chr}#{json}"
raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256
raise APN::Errors::ExceededMessageSizeError.new("#{message} #{message.size.to_i}") if message.size.to_i > 256
message
end

def self.send_notifications
ActiveSupport::Deprecation.warn("The method APN::Notification.send_notifications is deprecated. Use APN::App.send_notifications instead.")
APN::App.send_notifications
end
end # APN::Notification

end # APN::Notification