diff --git a/.gitignore b/.gitignore index 5bdf665bc..58cd81e15 100644 --- a/.gitignore +++ b/.gitignore @@ -76,4 +76,4 @@ yarn-debug.log* .yarn-integrity /storybook-static -config/chatwoot-3b1a1-c9a7423d7411.json \ No newline at end of file +config/chatwoot-fcm-service-account-file.json \ No newline at end of file diff --git a/app/services/notification/fcm_service.rb b/app/services/notification/fcm_service.rb index 94a270feb..3074e6e69 100644 --- a/app/services/notification/fcm_service.rb +++ b/app/services/notification/fcm_service.rb @@ -19,7 +19,7 @@ class FCMService end def token_expired? - Time.now >= @token_info[:expires_at] + Time.zone.now >= @token_info[:expires_at] end def generate_token @@ -30,7 +30,7 @@ class FCMService token = authorizer.fetch_access_token! { token: token['access_token'], - expires_at: Time.now + token['expires_in'].to_i + expires_at: Time.zone.now + token['expires_in'].to_i } end end diff --git a/app/services/notification/push_notification_service.rb b/app/services/notification/push_notification_service.rb index 1c4920820..339a3e28e 100644 --- a/app/services/notification/push_notification_service.rb +++ b/app/services/notification/push_notification_service.rb @@ -79,26 +79,10 @@ class Notification::PushNotificationService fcm = fcm_service.fcm_client message = { 'token': subscription.subscription_attributes['push_token'], - 'data': { - payload: { - data: { - id: 1 - } - }.to_json - }, - 'notification': { - title: notification.push_message_title, - body: notification.push_message_body - }, - 'android': {}, - 'apns': { - payload: { - aps: { - sound: 'default', - category: "#{Time.zone.now.to_i}" - } - } - }, + 'data': fcm_data, + 'notification': fcm_notification, + 'android': fcm_android_options, + 'apns': fcm_apns_options, 'fcm_options': { analytics_label: 'Label' } @@ -107,7 +91,6 @@ class Notification::PushNotificationService remove_subscription_if_error(subscription, response) end - def send_push_via_chatwoot_hub(subscription) return if ENV['FCM_SERVER_KEY'] return unless ActiveModel::Type::Boolean.new.cast(ENV.fetch('ENABLE_PUSH_RELAY_SERVER', true)) @@ -132,4 +115,38 @@ class Notification::PushNotificationService collapse_key: "chatwoot_#{notification.primary_actor_type.downcase}_#{notification.primary_actor_id}" } end + + def fcm_data + { + payload: { + data: { + notification: notification.fcm_push_data + } + }.to_json + } + end + + def fcm_notification + { + title: notification.push_message_title, + body: notification.push_message_body + } + end + + def fcm_android_options + { + priority: 'high' + } + end + + def fcm_apns_options + { + payload: { + aps: { + sound: 'default', + category: Time.zone.now.to_i.to_s + } + } + } + end end