Class: NoticedFcmBase

Inherits:
Noticed::Base
  • Object
show all
Defined in:
app/notifications/noticed_fcm_base.rb

Instance Method Summary collapse

Instance Method Details

#android_settingsObject



46
47
48
49
50
51
52
53
54
# File 'app/notifications/noticed_fcm_base.rb', line 46

def android_settings
  return if image_url.nil?

  {
    notification: {
      image: image_url
    }
  }
end

#apns_settingsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/notifications/noticed_fcm_base.rb', line 56

def apns_settings
  return if image_url.nil?

  {
    payload: {
      aps: {
        'mutable-content': 1
      }
    },
    fcm_options: {
      image: image_url
    }
  }
end

#cleanup_device_token(token:, platform:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



110
111
112
# File 'app/notifications/noticed_fcm_base.rb', line 110

def cleanup_device_token(token:, platform:)
  SpreeCmCommissioner::DeviceToken.where(registration_token: token).destroy_all
end

#convert_hash_values_to_str(hash) ⇒ Object



75
76
77
78
79
# File 'app/notifications/noticed_fcm_base.rb', line 75

def convert_hash_values_to_str(hash)
  hash.each do |key, value|
    hash[key] = value.to_s
  end
end

#extra_payloadObject



89
90
91
# File 'app/notifications/noticed_fcm_base.rb', line 89

def extra_payload
  {}
end

#fcm_credentialsObject



19
20
21
# File 'app/notifications/noticed_fcm_base.rb', line 19

def fcm_credentials
  Rails.application.credentials.
end

#fcm_device_tokens(recipient) ⇒ Object



23
24
25
# File 'app/notifications/noticed_fcm_base.rb', line 23

def fcm_device_tokens(recipient)
  recipient.device_tokens.map(&:registration_token)
end

#format_for_databaseObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/notifications/noticed_fcm_base.rb', line 6

def format_for_database
  {
    notificable: notificable,
    type: type,

    params: {
      payload: payload,
      translatable_options: translatable_options

    }
  }
end

#format_notification(device_token) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/notifications/noticed_fcm_base.rb', line 27

def format_notification(device_token)
  {
    data: convert_hash_values_to_str(payload),
    token: device_token,
    notification: {
      title: title,
      body: message
    },

    # send notication with image
    android: android_settings,
    apns: apns_settings
  }
end

#image_urlObject



42
43
44
# File 'app/notifications/noticed_fcm_base.rb', line 42

def image_url
  nil
end

#messageObject



97
98
99
# File 'app/notifications/noticed_fcm_base.rb', line 97

def message
  t('.message', **(record.params[:translatable_options] || {}))
end

#notificableObject

Raises:

  • (NotImplementedError)


71
72
73
# File 'app/notifications/noticed_fcm_base.rb', line 71

def notificable
  raise NotImplementedError, 'Notification must implement a notificable method'
end

#payloadObject



81
82
83
84
85
86
87
# File 'app/notifications/noticed_fcm_base.rb', line 81

def payload
  default_payload = {
    id: notificable.id.to_s,
    type: type
  }
  default_payload.merge(extra_payload)
end

#titleObject



101
102
103
# File 'app/notifications/noticed_fcm_base.rb', line 101

def title
  t('.title', **(record.params[:translatable_options] || {}))
end

#translatable_optionsObject



93
94
95
# File 'app/notifications/noticed_fcm_base.rb', line 93

def translatable_options
  {}
end

#typeObject



105
106
107
# File 'app/notifications/noticed_fcm_base.rb', line 105

def type
  self.class.to_s.underscore
end