chore: more cleanups

This commit is contained in:
Muhsin Keloth
2024-06-11 13:02:02 +05:30
parent 410eeeb610
commit e622375b23
2 changed files with 15 additions and 5 deletions
+13 -3
View File
@@ -1,13 +1,14 @@
class FCMService
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'].freeze
def initialize(project_id)
def initialize(project_id, credentials)
@project_id = project_id
@credentials = credentials
@token_info = nil
end
def fcm_client
FCM.new(current_token, credentials_json, @project_id)
FCM.new(current_token, credentials_path, @project_id)
end
private
@@ -23,7 +24,7 @@ class FCMService
def generate_token
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: StringIO.new(GlobalConfigService.load('FIREBASE_CREDENTIALS', nil)),
json_key_io: File.open(credentials_path),
scope: SCOPES
)
token = authorizer.fetch_access_token!
@@ -32,4 +33,13 @@ class FCMService
expires_at: Time.zone.now + token['expires_in'].to_i
}
end
def credentials_path
file = Tempfile.new(['firebase', '.json'])
file.write(@credentials)
file.rewind
file.path
ensure
file.close
end
end
@@ -71,11 +71,11 @@ class Notification::PushNotificationService
end
def send_fcm_push(subscription)
return unless ENV['FIREBASE_PROJECT_ID']
return unless GlobalConfigService.load('FIREBASE_PROJECT_ID', nil) && GlobalConfigService.load('FIREBASE_CREDENTIALS', nil)
return unless subscription.fcm?
fcm_service = FCMService.new(
ENV.fetch('FIREBASE_PROJECT_ID', nil)
GlobalConfigService.load('FIREBASE_PROJECT_ID', nil), GlobalConfigService.load('FIREBASE_CREDENTIALS', nil)
)
fcm = fcm_service.fcm_client
message = {