From e622375b2343c867faa32aa6fb88a902f77827db Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 11 Jun 2024 13:02:02 +0530 Subject: [PATCH] chore: more cleanups --- app/services/notification/fcm_service.rb | 16 +++++++++++++--- .../notification/push_notification_service.rb | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/services/notification/fcm_service.rb b/app/services/notification/fcm_service.rb index 004654988..2de5a7de1 100644 --- a/app/services/notification/fcm_service.rb +++ b/app/services/notification/fcm_service.rb @@ -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 diff --git a/app/services/notification/push_notification_service.rb b/app/services/notification/push_notification_service.rb index 6990d2559..3ea907781 100644 --- a/app/services/notification/push_notification_service.rb +++ b/app/services/notification/push_notification_service.rb @@ -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 = {