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.



116
117
118
# File 'lib/imobile/push_notification.rb', line 116

def certificate
  @certificate
end

#socketObject (readonly)

The raw SSL connection to the APNs.



114
115
116
# File 'lib/imobile/push_notification.rb', line 114

def socket
  @socket
end

Instance Method Details

#closeObject

Closes the APNs connection. The context is unusable afterwards.



108
109
110
111
# File 'lib/imobile/push_notification.rb', line 108

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

#closed?Boolean

True if the context’s APNs connection is closed.

Returns:

  • (Boolean)


119
120
121
# File 'lib/imobile/push_notification.rb', line 119

def closed?
  @closed
end

#finalizeObject

Called when the context is garbage-collected.

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



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

def finalize
  close unless @closed
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