mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-03 20:48:07 +00:00
Chore: Firebase Cloud Messaging Support (#858)
This commit is contained in:
@@ -102,6 +102,9 @@ CHARGEBEE_WEBHOOK_PASSWORD=
|
|||||||
## generate a new key value here : https://d3v.one/vapid-key-generator/
|
## generate a new key value here : https://d3v.one/vapid-key-generator/
|
||||||
# VAPID_PUBLIC_KEY=
|
# VAPID_PUBLIC_KEY=
|
||||||
# VAPID_PRIVATE_KEY=
|
# VAPID_PRIVATE_KEY=
|
||||||
|
#
|
||||||
|
# for mobile apps
|
||||||
|
# FCM_SERVER_KEY=
|
||||||
|
|
||||||
## Bot Customizations
|
## Bot Customizations
|
||||||
USE_INBOX_AVATAR_FOR_BOT=true
|
USE_INBOX_AVATAR_FOR_BOT=true
|
||||||
|
|||||||
1
Gemfile
1
Gemfile
@@ -84,6 +84,7 @@ gem 'sidekiq'
|
|||||||
gem 'flag_shih_tzu'
|
gem 'flag_shih_tzu'
|
||||||
|
|
||||||
##-- Push notification service --##
|
##-- Push notification service --##
|
||||||
|
gem 'fcm'
|
||||||
gem 'webpush'
|
gem 'webpush'
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
|
|||||||
@@ -189,6 +189,8 @@ GEM
|
|||||||
multipart-post (>= 1.2, < 3)
|
multipart-post (>= 1.2, < 3)
|
||||||
faraday_middleware (1.0.0)
|
faraday_middleware (1.0.0)
|
||||||
faraday (~> 1.0)
|
faraday (~> 1.0)
|
||||||
|
fcm (1.0.1)
|
||||||
|
faraday (~> 1.0.0)
|
||||||
ffi (1.12.2)
|
ffi (1.12.2)
|
||||||
flag_shih_tzu (0.3.23)
|
flag_shih_tzu (0.3.23)
|
||||||
foreman (0.87.1)
|
foreman (0.87.1)
|
||||||
@@ -547,6 +549,7 @@ DEPENDENCIES
|
|||||||
facebook-messenger
|
facebook-messenger
|
||||||
factory_bot_rails
|
factory_bot_rails
|
||||||
faker
|
faker
|
||||||
|
fcm
|
||||||
flag_shih_tzu
|
flag_shih_tzu
|
||||||
foreman
|
foreman
|
||||||
google-cloud-storage
|
google-cloud-storage
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ class NotificationSubscriptionBuilder
|
|||||||
|
|
||||||
def identifier
|
def identifier
|
||||||
@identifier ||= params[:subscription_attributes][:endpoint] if params[:subscription_type] == 'browser_push'
|
@identifier ||= params[:subscription_attributes][:endpoint] if params[:subscription_type] == 'browser_push'
|
||||||
|
@identifier ||= params[:device_id] if params[:subscription_type] == 'fcm'
|
||||||
|
@identifier
|
||||||
end
|
end
|
||||||
|
|
||||||
def identifier_subscription
|
def identifier_subscription
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class NotificationSubscription < ApplicationRecord
|
|||||||
|
|
||||||
SUBSCRIPTION_TYPES = {
|
SUBSCRIPTION_TYPES = {
|
||||||
browser_push: 1,
|
browser_push: 1,
|
||||||
gcm: 2
|
fcm: 2
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
enum subscription_type: SUBSCRIPTION_TYPES
|
enum subscription_type: SUBSCRIPTION_TYPES
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ class Notification::PushNotificationService
|
|||||||
return unless user_subscribed_to_notification?
|
return unless user_subscribed_to_notification?
|
||||||
|
|
||||||
notification_subscriptions.each do |subscription|
|
notification_subscriptions.each do |subscription|
|
||||||
send_browser_push(subscription) if subscription.browser_push?
|
send_browser_push(subscription)
|
||||||
|
send_fcm_push(subscription)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -53,6 +54,8 @@ class Notification::PushNotificationService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def send_browser_push(subscription)
|
def send_browser_push(subscription)
|
||||||
|
return unless subscription.browser_push?
|
||||||
|
|
||||||
Webpush.payload_send(
|
Webpush.payload_send(
|
||||||
message: JSON.generate(push_message),
|
message: JSON.generate(push_message),
|
||||||
endpoint: subscription.subscription_attributes['endpoint'],
|
endpoint: subscription.subscription_attributes['endpoint'],
|
||||||
@@ -70,4 +73,17 @@ class Notification::PushNotificationService
|
|||||||
rescue Webpush::ExpiredSubscription
|
rescue Webpush::ExpiredSubscription
|
||||||
subscription.destroy!
|
subscription.destroy!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send_fcm_push(subscription)
|
||||||
|
return unless subscription.fcm?
|
||||||
|
|
||||||
|
fcm = FCM.new(ENV['FCM_SERVER_KEY'])
|
||||||
|
options = { "notification": {
|
||||||
|
"title": notification.notification_type.titleize,
|
||||||
|
"body": push_message_title,
|
||||||
|
"notification": notification.to_json
|
||||||
|
} }
|
||||||
|
response = fcm.send([subscription.subscription_attributes['push_token']], options)
|
||||||
|
subscription.destroy! if JSON.parse(response[:body])['results']&.first&.keys&.include?('error')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
32
spec/services/notification/push_notification_service_spec.rb
Normal file
32
spec/services/notification/push_notification_service_spec.rb
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Notification::PushNotificationService do
|
||||||
|
let!(:account) { create(:account) }
|
||||||
|
let!(:user) { create(:user, account: account) }
|
||||||
|
let!(:notification) { create(:notification, user: user, account: user.accounts.first) }
|
||||||
|
let(:fcm_double) { double }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(Webpush).to receive(:payload_send).and_return(true)
|
||||||
|
allow(FCM).to receive(:new).and_return(fcm_double)
|
||||||
|
allow(fcm_double).to receive(:send).and_return({ body: { 'results': [] }.to_json })
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#perform' do
|
||||||
|
it 'sends webpush notifications for webpush subscription' do
|
||||||
|
create(:notification_subscription, user: notification.user)
|
||||||
|
|
||||||
|
described_class.new(notification: notification).perform
|
||||||
|
expect(Webpush).to have_received(:payload_send)
|
||||||
|
expect(FCM).not_to have_received(:new)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sends a fcm notification for firebase subscription' do
|
||||||
|
create(:notification_subscription, user: notification.user, subscription_type: 'fcm')
|
||||||
|
|
||||||
|
described_class.new(notification: notification).perform
|
||||||
|
expect(FCM).to have_received(:new)
|
||||||
|
expect(Webpush).not_to have_received(:payload_send)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user