mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-30 18:47:51 +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/ | ||||
| # VAPID_PUBLIC_KEY= | ||||
| # VAPID_PRIVATE_KEY= | ||||
| # | ||||
| # for mobile apps  | ||||
| # FCM_SERVER_KEY= | ||||
|  | ||||
| ## Bot Customizations | ||||
| USE_INBOX_AVATAR_FOR_BOT=true | ||||
|   | ||||
							
								
								
									
										1
									
								
								Gemfile
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								Gemfile
									
									
									
									
									
								
							| @@ -84,6 +84,7 @@ gem 'sidekiq' | ||||
| gem 'flag_shih_tzu' | ||||
|  | ||||
| ##-- Push notification service --## | ||||
| gem 'fcm' | ||||
| gem 'webpush' | ||||
|  | ||||
| group :development do | ||||
|   | ||||
| @@ -189,6 +189,8 @@ GEM | ||||
|       multipart-post (>= 1.2, < 3) | ||||
|     faraday_middleware (1.0.0) | ||||
|       faraday (~> 1.0) | ||||
|     fcm (1.0.1) | ||||
|       faraday (~> 1.0.0) | ||||
|     ffi (1.12.2) | ||||
|     flag_shih_tzu (0.3.23) | ||||
|     foreman (0.87.1) | ||||
| @@ -547,6 +549,7 @@ DEPENDENCIES | ||||
|   facebook-messenger | ||||
|   factory_bot_rails | ||||
|   faker | ||||
|   fcm | ||||
|   flag_shih_tzu | ||||
|   foreman | ||||
|   google-cloud-storage | ||||
|   | ||||
| @@ -12,6 +12,8 @@ class NotificationSubscriptionBuilder | ||||
|  | ||||
|   def identifier | ||||
|     @identifier ||= params[:subscription_attributes][:endpoint] if params[:subscription_type] == 'browser_push' | ||||
|     @identifier ||= params[:device_id] if params[:subscription_type] == 'fcm' | ||||
|     @identifier | ||||
|   end | ||||
|  | ||||
|   def identifier_subscription | ||||
|   | ||||
| @@ -22,7 +22,7 @@ class NotificationSubscription < ApplicationRecord | ||||
|  | ||||
|   SUBSCRIPTION_TYPES = { | ||||
|     browser_push: 1, | ||||
|     gcm: 2 | ||||
|     fcm: 2 | ||||
|   }.freeze | ||||
|  | ||||
|   enum subscription_type: SUBSCRIPTION_TYPES | ||||
|   | ||||
| @@ -7,7 +7,8 @@ class Notification::PushNotificationService | ||||
|     return unless user_subscribed_to_notification? | ||||
|  | ||||
|     notification_subscriptions.each do |subscription| | ||||
|       send_browser_push(subscription) if subscription.browser_push? | ||||
|       send_browser_push(subscription) | ||||
|       send_fcm_push(subscription) | ||||
|     end | ||||
|   end | ||||
|  | ||||
| @@ -53,6 +54,8 @@ class Notification::PushNotificationService | ||||
|   end | ||||
|  | ||||
|   def send_browser_push(subscription) | ||||
|     return unless subscription.browser_push? | ||||
|  | ||||
|     Webpush.payload_send( | ||||
|       message: JSON.generate(push_message), | ||||
|       endpoint: subscription.subscription_attributes['endpoint'], | ||||
| @@ -70,4 +73,17 @@ class Notification::PushNotificationService | ||||
|   rescue Webpush::ExpiredSubscription | ||||
|     subscription.destroy! | ||||
|   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 | ||||
|   | ||||
							
								
								
									
										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
	 Sojan Jose
					Sojan Jose