Module: Rpush::Client::ActiveModel::Apns::Notification
- Included in:
- Rpush::Client::ActiveRecord::Apns2::Notification, Rpush::Client::ActiveRecord::Apns::Notification, Redis::Apns2::Notification, Redis::Apns::Notification, Redis::Apnsp8::Notification
- Defined in:
- lib/rpush/client/active_model/apns/notification.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- APNS_DEFAULT_EXPIRY =
1.day.to_i
- APNS_PRIORITY_IMMEDIATE =
10
- APNS_PRIORITY_CONSERVE_POWER =
5
- APNS_PRIORITIES =
[APNS_PRIORITY_IMMEDIATE, APNS_PRIORITY_CONSERVE_POWER]
- MAX_PAYLOAD_BYTESIZE =
2048
- MDM_KEY =
'__rpush_mdm__'
- MUTABLE_CONTENT_KEY =
'__rpush_mutable_content__'
- CONTENT_AVAILABLE_KEY =
'__rpush_content_available__'
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity.
- #content_available=(bool) ⇒ Object
- #device_token=(token) ⇒ Object
- #mdm=(magic) ⇒ Object
- #mutable_content=(bool) ⇒ Object
- #to_binary(options = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 18 def self.included(base) base.extend ClassMethods base.instance_eval do validates :device_token, presence: true validates :badge, numericality: true, allow_nil: true validates :priority, inclusion: { in: APNS_PRIORITIES }, allow_nil: true validates_with Rpush::Client::ActiveModel::Apns::DeviceTokenFormatValidator validates_with Rpush::Client::ActiveModel::Apns::NotificationPayloadSizeValidator base.const_set('APNS_DEFAULT_EXPIRY', APNS_DEFAULT_EXPIRY) unless base.const_defined?('APNS_DEFAULT_EXPIRY') base.const_set('APNS_PRIORITY_IMMEDIATE', APNS_PRIORITY_IMMEDIATE) unless base.const_defined?('APNS_PRIORITY_IMMEDIATE') base.const_set('APNS_PRIORITY_CONSERVE_POWER', APNS_PRIORITY_CONSERVE_POWER) unless base.const_defined?('APNS_PRIORITY_CONSERVE_POWER') end end |
Instance Method Details
#as_json(options = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 55 def as_json( = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity json = ActiveSupport::OrderedHash.new if data && data.key?(MDM_KEY) json['mdm'] = data[MDM_KEY] else json['aps'] = ActiveSupport::OrderedHash.new json['aps']['alert'] = alert if alert json['aps']['badge'] = badge if badge json['aps']['sound'] = sound if sound json['aps']['category'] = category if category json['aps']['url-args'] = url_args if url_args json['aps']['thread-id'] = thread_id if thread_id if data && data[MUTABLE_CONTENT_KEY] json['aps']['mutable-content'] = 1 end if data && data[CONTENT_AVAILABLE_KEY] json['aps']['content-available'] = 1 end if data non_aps_attributes = data.reject { |k, _| k == CONTENT_AVAILABLE_KEY || k == MUTABLE_CONTENT_KEY } non_aps_attributes.each { |k, v| json[k.to_s] = v } end end json end |
#content_available=(bool) ⇒ Object
50 51 52 53 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 50 def content_available=(bool) return unless bool self.data = (data || {}).merge(CONTENT_AVAILABLE_KEY => true) end |
#device_token=(token) ⇒ Object
34 35 36 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 34 def device_token=(token) write_attribute(:device_token, token.delete(" <>")) unless token.nil? end |
#mdm=(magic) ⇒ Object
39 40 41 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 39 def mdm=(magic) self.data = (data || {}).merge(MDM_KEY => magic) end |
#mutable_content=(bool) ⇒ Object
44 45 46 47 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 44 def mutable_content=(bool) return unless bool self.data = (data || {}).merge(MUTABLE_CONTENT_KEY => true) end |
#to_binary(options = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rpush/client/active_model/apns/notification.rb', line 86 def to_binary( = {}) frame_payload = payload frame_id = [:for_validation] ? 0 : send(.fetch(:id_attribute, :id)) frame = "" frame << [1, 32, device_token].pack("cnH*") frame << [2, frame_payload.bytesize, frame_payload].pack("cna*") frame << [3, 4, frame_id].pack("cnN") frame << [4, 4, expiry ? Time.now.to_i + expiry.to_i : 0].pack("cnN") frame << [5, 1, priority_for_frame].pack("cnc") [2, frame.bytesize].pack("cN") + frame end |