Class: Imobile::PushNotificationsContext

Inherits:
Object
  • Object
show all
Defined in:
lib/imobile/push_notification.rb

Overview

Carries state for delivering batched notifications.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_or_certificate) ⇒ PushNotificationsContext

Creates a push context for a fixed Apple Push Notifications server.

Args:

path_or_certificate:: see Imobile.push_notification


101
102
103
104
105
# File 'lib/imobile/push_notification.rb', line 101

def initialize(path_or_certificate)
  @certificate = PushNotifications.read_certificate path_or_certificate    
  @socket = PushNotifications.apns_socket @certificate, :push
  @closed = false
end

Instance Attribute Details

#certificateObject (readonly)

The APNs client certificate.



128
129
130
# File 'lib/imobile/push_notification.rb', line 128

def certificate
  @certificate
end

#socketObject (readonly)

The raw SSL connection to the APNs.



126
127
128
# File 'lib/imobile/push_notification.rb', line 126

def socket
  @socket
end

Instance Method Details

#closeObject

Closes the APNs connection. The context is unusable afterwards.



117
118
119
120
121
122
123
# File 'lib/imobile/push_notification.rb', line 117

def close
  unless @closed
    @socket.flush
    @socket.close
  end      
  @closed = true
end

#closed?Boolean

True if the context’s APNs connection is closed.

Returns:

  • (Boolean)


131
132
133
# File 'lib/imobile/push_notification.rb', line 131

def closed?
  @closed
end

#finalizeObject

Called when the context is garbage-collected.

Closes the APNs connection, if it wasn’t already closed.



138
139
140
# File 'lib/imobile/push_notification.rb', line 138

def finalize
  close unless @closed
end

#flushObject

Forces a flush of all the buffered data across the APNs connection.

This should be called at the end of a batch of notifications. If flush is not called, notifications may be stalled indefinitely.



111
112
113
114
# File 'lib/imobile/push_notification.rb', line 111

def flush
  raise "The context's APNs connection was closed" if @closed
  @socket.flush
end

#push(notification) ⇒ Object

Sends a notification via this context’s APNs connection.

Args:

notification:: see Imobile.push_notification

Raises a RuntimeError if the context’s APNs connection was closed.



92
93
94
95
# File 'lib/imobile/push_notification.rb', line 92

def push(notification)
  raise "The context's APNs connection was closed" if @closed
  @socket.write PushNotifications.encode_notification(notification)
end