Module: Rpush::Client::ActiveModel::Gcm::Notification

Included in:
Rpush::Client::ActiveRecord::Gcm::Notification, Redis::Gcm::Notification
Defined in:
lib/rpush/client/active_model/gcm/notification.rb

Constant Summary collapse

GCM_PRIORITY_HIGH =
Rpush::Client::ActiveModel::Apns::Notification::APNS_PRIORITY_IMMEDIATE
GCM_PRIORITY_NORMAL =
Rpush::Client::ActiveModel::Apns::Notification::APNS_PRIORITY_CONSERVE_POWER
GCM_PRIORITIES =
[GCM_PRIORITY_HIGH, GCM_PRIORITY_NORMAL]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rpush/client/active_model/gcm/notification.rb', line 10

def self.included(base)
  base.instance_eval do
    validates :registration_ids, presence: true
    validates :priority, inclusion: { in: GCM_PRIORITIES }, allow_nil: true
    validates :dry_run, inclusion: { in: [true, false] }

    validates_with Rpush::Client::ActiveModel::PayloadDataSizeValidator, limit: 4096
    validates_with Rpush::Client::ActiveModel::RegistrationIdsCountValidator, limit: 1000

    validates_with Rpush::Client::ActiveModel::Gcm::ExpiryCollapseKeyMutualInclusionValidator
  end
end

Instance Method Details

#as_json(options = nil) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rpush/client/active_model/gcm/notification.rb', line 38

def as_json(options = nil) # rubocop:disable Metrics/PerceivedComplexity
  json = {
      'registration_ids' => registration_ids,
      'delay_while_idle' => delay_while_idle,
      'data' => data
  }
  json['collapse_key'] = collapse_key if collapse_key
  json['content_available'] = content_available if content_available
  json['mutable_content'] = mutable_content if mutable_content
  json['dry_run'] = dry_run if dry_run
  json['notification'] = notification if notification
  json['priority'] = priority_for_notification if priority
  json['time_to_live'] = expiry if expiry
  json
end

#priority=(priority) ⇒ Object

This is a hack. The schema defines `priority` to be an integer, but GCM expects a string. But for users of rpush to have an API they might expect (setting priority to `high`, not 10) we do a little conversion here. I'm not happy about it, but this will have to do until I can take a further look.


27
28
29
30
31
32
33
34
35
36
# File 'lib/rpush/client/active_model/gcm/notification.rb', line 27

def priority=(priority)
  case priority
    when 'high', GCM_PRIORITY_HIGH
      super(GCM_PRIORITY_HIGH)
    when 'normal', GCM_PRIORITY_NORMAL
      super(GCM_PRIORITY_NORMAL)
    else
      errors.add(:priority, 'must be one of either "normal" or "high"')
  end
end

#priority_for_notificationObject


54
55
56
57
# File 'lib/rpush/client/active_model/gcm/notification.rb', line 54

def priority_for_notification
  return 'high' if priority == GCM_PRIORITY_HIGH
  'normal' if priority == GCM_PRIORITY_NORMAL
end