feat: FCM HTTP v1 API changes (#9629)

Fixes https://linear.app/chatwoot/issue/CW-3210/legacy-firebase-changes
This commit is contained in:
Muhsin Keloth
2024-06-18 10:38:06 +05:30
committed by GitHub
parent 7968e98529
commit 9046730206
7 changed files with 196 additions and 24 deletions

View File

@@ -0,0 +1,40 @@
class Notification::FcmService
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'].freeze
def initialize(project_id, credentials)
@project_id = project_id
@credentials = credentials
@token_info = nil
end
def fcm_client
FCM.new(current_token, credentials_path, @project_id)
end
private
def current_token
@token_info = generate_token if @token_info.nil? || token_expired?
@token_info[:token]
end
def token_expired?
Time.zone.now >= @token_info[:expires_at]
end
def generate_token
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: credentials_path,
scope: SCOPES
)
token = authorizer.fetch_access_token!
{
token: token['access_token'],
expires_at: Time.zone.now + token['expires_in'].to_i
}
end
def credentials_path
StringIO.new(@credentials)
end
end