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
# 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_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



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

def as_json(options = nil)
  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['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.



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

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

#priority_for_notificationObject



51
52
53
54
# File 'lib/rpush/client/active_model/gcm/notification.rb', line 51

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