Class: Noticed::DeliveryMethods::Fcm
- Inherits:
-
Base
- Object
- Base
- Noticed::DeliveryMethods::Fcm
show all
- Defined in:
- lib/noticed/delivery_methods/fcm.rb
Constant Summary
collapse
- BASE_URI =
"https://fcm.googleapis.com/v1/projects/"
Instance Attribute Summary
Attributes inherited from Base
#recipient, #record
Instance Method Summary
collapse
Methods inherited from Base
#clear_recipient, deliver, deliver_by, deliver_later, #deliver_later, inherited, #initialize, param, params, #params, with
#class_scope, #i18n_scope, #scope_translation_key, #translate
Constructor Details
This class inherits a constructor from Noticed::Base
Instance Method Details
#access_token ⇒ Object
64
65
66
67
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 64
def access_token
token = authorizer.fetch_access_token!
token["access_token"]
end
|
#authorizer ⇒ Object
69
70
71
72
73
74
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 69
def authorizer
@authorizer ||= options.fetch(:authorizer, Google::Auth::ServiceAccountCredentials).make_creds(
json_key_io: StringIO.new(credentials.to_json),
scope: "https://www.googleapis.com/auth/firebase.messaging"
)
end
|
#cleanup_invalid_token(device_token) ⇒ Object
31
32
33
34
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 31
def cleanup_invalid_token(device_token)
return unless notification.respond_to?(:cleanup_device_token)
notification.send(:cleanup_device_token, token: device_token, platform: "fcm")
end
|
#credentials ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 36
def credentials
@credentials ||= begin
option = options[:credentials]
credentials_hash = case option
when Hash
option
when Pathname
load_json(option)
when String
load_json(Rails.root.join(option))
when Symbol
notification.send(option)
else
Rails.application.credentials.fcm
end
credentials_hash.symbolize_keys
end
end
|
#deliver ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 19
def deliver
device_tokens.each do |device_token|
post("#{BASE_URI}#{project_id}/messages:send", headers: {authorization: "Bearer #{access_token}"}, json: {message: format(device_token)})
rescue ResponseUnsuccessful => exception
if exception.response.code == 404
cleanup_invalid_token(device_token)
else
raise
end
end
end
|
#device_tokens ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 80
def device_tokens
if notification.respond_to?(:fcm_device_tokens)
Array.wrap(notification.fcm_device_tokens(recipient))
else
raise NoMethodError, <<~MESSAGE
You must implement `fcm_device_tokens` to send Firebase Cloud Messaging notifications
# This must return an Array of FCM device tokens
def fcm_device_tokens(recipient)
recipient.fcm_device_tokens.pluck(:token)
end
MESSAGE
end
end
|
76
77
78
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 76
def format(device_token)
notification.send(options[:format], device_token)
end
|
#load_json(path) ⇒ Object
56
57
58
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 56
def load_json(path)
JSON.parse(File.read(path))
end
|
#project_id ⇒ Object
60
61
62
|
# File 'lib/noticed/delivery_methods/fcm.rb', line 60
def project_id
credentials[:project_id]
end
|