module Rpush::Client::ActiveModel::Fcm::Notification

Constants

ANDROID_NOTIFICATION_KEYS
FCM_PRIORITIES
FCM_PRIORITY_HIGH
FCM_PRIORITY_NORMAL
ROOT_NOTIFICATION_KEYS

Public Class Methods

included(base) click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 16
def self.included(base)
  base.instance_eval do
    validates :device_token, presence: true
    validates :priority, inclusion: { in: FCM_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::Fcm::ExpiryCollapseKeyMutualInclusionValidator
    validates_with Rpush::Client::ActiveModel::Fcm::NotificationKeysInAllowedListValidator
  end
end

Public Instance Methods

android_config() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 63
def android_config
  json = ActiveSupport::OrderedHash.new
  json['notification'] = android_notification if notification
  json['collapse_key'] = collapse_key if collapse_key
  json['priority'] = priority_str if priority
  json['ttl'] = "#{expiry}s" if expiry
  json
end
android_notification() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 98
def android_notification
  json = notification&.slice(*ANDROID_NOTIFICATION_KEYS) || {}
  json['notification_priority'] = priority_for_notification if priority
  json['sound'] = sound if sound
  json['default_sound'] = sound == 'default' ? true : false
  json
end
apns_config() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 72
def apns_config
  json = ActiveSupport::OrderedHash.new
  json['payload'] = ActiveSupport::OrderedHash.new

  aps = ActiveSupport::OrderedHash.new
  aps['mutable-content'] = 1 if mutable_content
  aps['content-available'] = 1 if content_available
  aps['sound'] = 'default' if sound == 'default'
  aps['badge'] = badge if badge

  json['payload']['aps'] = aps

  json
end
as_json(options = nil) click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 51
def as_json(options = nil) # rubocop:disable Metrics/PerceivedComplexity
  json = {
    'data' => data,
    'android' => android_config,
    'apns' => apns_config,
    'token' => device_token
  }

  json['notification'] = root_notification if notification
  { 'message' => json }
end
dry_run=(value) click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 47
def dry_run=(value)
  fail ArgumentError, 'FCM does not support dry run' if value
end
notification=(value) click to toggle source
Calls superclass method
# File lib/rpush/client/active_model/fcm/notification.rb, line 87
def notification=(value)
  value = value.with_indifferent_access if value.is_a?(Hash)
  super(value)
end
payload_data_size() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 29
def payload_data_size
  multi_json_dump(as_json['message']['data']).bytesize
end
priority=(priority) click to toggle source

This is a hack. The schema defines ‘priority` to be an integer, but FCM 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.

Calls superclass method
# File lib/rpush/client/active_model/fcm/notification.rb, line 36
def priority=(priority)
  case priority
    when 'high', FCM_PRIORITY_HIGH
      super(FCM_PRIORITY_HIGH)
    when 'normal', FCM_PRIORITY_NORMAL
      super(FCM_PRIORITY_NORMAL)
    else
      errors.add(:priority, 'must be one of either "normal" or "high"')
  end
end
priority_for_notification() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 114
def priority_for_notification
  case priority
  when 0 then 'PRIORITY_UNSPECIFIED'
  when 1 then 'PRIORITY_MIN'
  when 2 then 'PRIORITY_LOW'
  when 5 then 'PRIORITY_DEFAULT'
  when 6 then 'PRIORITY_HIGH'
  when 10 then 'PRIORITY_MAX'
  else
    'PRIORITY_DEFAULT'
  end
end
priority_str() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 106
def priority_str
  case
  when priority <= 5 then 'normal'
  else
    'high'
  end
end
root_notification() click to toggle source
# File lib/rpush/client/active_model/fcm/notification.rb, line 92
def root_notification
  return {} unless notification

  notification.slice(*ROOT_NOTIFICATION_KEYS)
end