Module: Rpush::Client::ActiveModel::Fcm::Notification

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

Constant Summary collapse

FCM_PRIORITY_HIGH =
Rpush::Client::ActiveModel::Apns::Notification::APNS_PRIORITY_IMMEDIATE
FCM_PRIORITY_NORMAL =
Rpush::Client::ActiveModel::Apns::Notification::APNS_PRIORITY_CONSERVE_POWER
FCM_PRIORITIES =
[FCM_PRIORITY_HIGH, FCM_PRIORITY_NORMAL]
ROOT_NOTIFICATION_KEYS =
%w[title body image].freeze
ANDROID_NOTIFICATION_KEYS =
%w[icon tag color click_action body_loc_key body_loc_args title_loc_key
title_loc_args channel_id ticker sticky event_time local_only
default_vibrate_timings default_light_settings vibrate_timings
visibility notification_count light_settings].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#android_configObject



63
64
65
66
67
68
69
70
# 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_notificationObject



98
99
100
101
102
103
104
# 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_configObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



87
88
89
90
# 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_sizeObject



29
30
31
# 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) ⇒ Object

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.



36
37
38
39
40
41
42
43
44
45
# 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_notificationObject



114
115
116
117
118
119
120
121
122
123
124
125
# 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_strObject



106
107
108
109
110
111
112
# 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_notificationObject



92
93
94
95
96
# File 'lib/rpush/client/active_model/fcm/notification.rb', line 92

def root_notification
  return {} unless notification

  notification.slice(*ROOT_NOTIFICATION_KEYS)
end