Class: APN::GroupNotification

Inherits:
Base
  • Object
show all
Extended by:
ActionView::Helpers::TextHelper
Includes:
ActionView::Helpers::TextHelper
Defined in:
lib/apn_on_rails/app/models/apn/group_notification.rb

Instance Method Summary collapse

Methods inherited from Base

table_name

Instance Method Details

#alert=(message) ⇒ Object

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 ...



20
21
22
23
24
25
# File 'lib/apn_on_rails/app/models/apn/group_notification.rb', line 20

def alert=(message)
  if !message.blank? && message.size > 150
    message = truncate(message, :length => 150)
  end
  write_attribute('alert', message)
end

#apple_hashObject

Creates a Hash that will be the payload of an APN.

Example:

apn = APN::GroupNotification.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:

apn = APN::GroupNotification.new
apn.badge = 0
apn.sound = true
apn.custom_properties = {"typ" => 1}
apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => 1.aiff},"typ" => "1"}


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/apn_on_rails/app/models/apn/group_notification.rb', line 42

def apple_hash
  result = {}
  result['aps'] = {}
  result['aps']['alert'] = self.alert if self.alert
  result['aps']['badge'] = self.badge.to_i if self.badge
  if self.sound
    result['aps']['sound'] = self.sound if self.sound.is_a? String
    result['aps']['sound'] = "1.aiff" if self.sound.is_a?(TrueClass)
  end
  if self.custom_properties
    self.custom_properties.each do |key,value|
      result["#{key}"] = "#{value}"
    end
  end
  result
end

#devicesObject



12
13
14
# File 'lib/apn_on_rails/app/models/apn/group_notification.rb', line 12

def devices
  self.group.devices
end

#message_for_sending(device) ⇒ Object

Creates the binary message needed to send to Apple.



72
73
74
75
76
77
# File 'lib/apn_on_rails/app/models/apn/group_notification.rb', line 72

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

#to_apple_jsonObject

Creates the JSON string required for an APN message.

Example:

apn = APN::Notification.new
apn.badge = 5
apn.sound = 'my_sound.aiff'
apn.alert = 'Hello!'
apn.to_apple_json # => '{"aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}'


67
68
69
# File 'lib/apn_on_rails/app/models/apn/group_notification.rb', line 67

def to_apple_json
  self.apple_hash.to_json
end