Module: APNS

Defined in:
lib/apns/core.rb

Overview

Copyright © 2009 James Pozdena, 2010 Justin.tv

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_connectionsObject

Returns the value of attribute cache_connections.



49
50
51
# File 'lib/apns/core.rb', line 49

def cache_connections
  @cache_connections
end

.feedback_hostObject

Returns the value of attribute feedback_host.



49
50
51
# File 'lib/apns/core.rb', line 49

def feedback_host
  @feedback_host
end

.feedback_portObject

Returns the value of attribute feedback_port.



49
50
51
# File 'lib/apns/core.rb', line 49

def feedback_port
  @feedback_port
end

.hostObject

Returns the value of attribute host.



49
50
51
# File 'lib/apns/core.rb', line 49

def host
  @host
end

.passObject

Returns the value of attribute pass.



49
50
51
# File 'lib/apns/core.rb', line 49

def pass
  @pass
end

.pemObject

Returns the value of attribute pem.



49
50
51
# File 'lib/apns/core.rb', line 49

def pem
  @pem
end

.portObject

Returns the value of attribute port.



49
50
51
# File 'lib/apns/core.rb', line 49

def port
  @port
end

Class Method Details

.establish_notification_connectionObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/apns/core.rb', line 52

def self.establish_notification_connection
  if @cache_connections
    begin
      self.get_connection(self.host, self.port, self.pem)
      return true
    rescue
    end
  end
  return false
end

.feedbackObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/apns/core.rb', line 121

def self.feedback
  apns_feedback = []
  self.with_feedback_connection do |conn|
    # Read buffers data from the OS, so it's probably not
    # too inefficient to do the small reads
    while data = conn.read(38)
      apns_feedback << self.parse_feedback_tuple(data)
    end
  end
  
  return apns_feedback
end

.has_notification_connection?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/apns/core.rb', line 63

def self.has_notification_connection?
  return self.has_connection?(self.host, self.port)
end

.send_notification(device_token, message) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/apns/core.rb', line 87

def self.send_notification(device_token, message)
  self.validate_message(message)
  self.with_notification_connection do |conn|
    conn.write(self.packaged_notification(device_token, message))
    conn.flush
  end
end

.send_notification_async(device_token, message) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/apns/core.rb', line 105

def self.send_notification_async(device_token, message)
  self.validate_message(message)
  thread = Thread.new do
    self.send_notification(device_token, message)
  end
  return thread
end

.send_notifications(notifications) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/apns/core.rb', line 95

def self.send_notifications(notifications)
  self.validate_messages(notifications)
  self.with_notification_connection do |conn|
    notifications.each do |n|
      conn.write(self.packaged_notification(n[0], n[1]))
    end
    conn.flush
  end
end

.send_notifications_async(notifications) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/apns/core.rb', line 113

def self.send_notifications_async(notifications)
  self.validate_messages(notifications)
  thread = Thread.new do
    self.send_notifications(notifications)
  end
  return thread
end

.validate_message(message) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/apns/core.rb', line 67

def self.validate_message(message)
  if message.is_a?(Hash)
    if message.to_json.bytesize > 256
      raise "Message(json) bytesize > 256"
    end
  elsif message.is_a?(String)
    if message.bytesize > 256
      raise "Message bytesize > 256"
    end
  else
    raise "Message needs to be either a hash or string"
  end
end

.validate_messages(notifications) ⇒ Object



81
82
83
84
85
# File 'lib/apns/core.rb', line 81

def self.validate_messages(notifications)
  notifications.each do |n|
    self.validate_message(n[1])
  end
end