Class: Imobile::PushNotificationsContext
- Inherits:
-
Object
- Object
- Imobile::PushNotificationsContext
- Defined in:
- lib/imobile/push_notification.rb
Overview
Carries state for delivering batched notifications.
Instance Attribute Summary collapse
-
#certificate ⇒ Object
readonly
The APNs client certificate.
-
#socket ⇒ Object
readonly
The raw SSL connection to the APNs.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the APNs connection.
-
#closed? ⇒ Boolean
True if the context’s APNs connection is closed.
-
#finalize ⇒ Object
Called when the context is garbage-collected.
-
#flush ⇒ Object
Forces a flush of all the buffered data across the APNs connection.
-
#initialize(path_or_certificate) ⇒ PushNotificationsContext
constructor
Creates a push context for a fixed Apple Push Notifications server.
-
#push(notification) ⇒ Object
Sends a notification via this context’s APNs connection.
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
#certificate ⇒ Object (readonly)
The APNs client certificate.
128 129 130 |
# File 'lib/imobile/push_notification.rb', line 128 def certificate @certificate end |
#socket ⇒ Object (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
#close ⇒ Object
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.
131 132 133 |
# File 'lib/imobile/push_notification.rb', line 131 def closed? @closed end |
#finalize ⇒ Object
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 |
#flush ⇒ Object
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 |