Class: Decidim::SendPushNotification
- Inherits:
-
Object
- Object
- Decidim::SendPushNotification
- Includes:
- ActionView::Helpers::UrlHelper
- Defined in:
- decidim-core/app/services/decidim/send_push_notification.rb
Overview
This class generates a notification based on the given event, for the given resource/recipient couple. It is intended to be used by the ‘Decidim::NotificationGenerator` class, which schedules a job for each recipient of the event, so that we can easily control which jobs fail.
Instance Method Summary collapse
-
#perform(notification, title = nil) ⇒ Array<Net::HTTPCreated>?
Send the push notification.
Instance Method Details
#perform(notification, title = nil) ⇒ Array<Net::HTTPCreated>?
Send the push notification. Returns ‘nil` if the user did not allowed push notifications or if the subscription to push notifications does not exist
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'decidim-core/app/services/decidim/send_push_notification.rb', line 21 def perform(notification, title = nil) return unless Rails.application.secrets.dig(:vapid, :enabled) raise ArgumentError, "Need to provide a title if the notification is a PushNotificationMessage" if notification.is_a?(Decidim::PushNotificationMessage) && title.nil? user = notification.user I18n.with_locale(user.locale || user.organization.default_locale) do user.notifications_subscriptions.values.map do |subscription| payload = build_payload((notification, title), subscription) # Capture webpush exceptions in order to avoid this call to be repeated by the background job runner # Webpush::Error class is the parent class of all defined errors begin WebPush.payload_send(**payload) rescue WebPush::Error => e Rails.logger.warn("[ERROR] Push notification delivery failed due to #{e.}") nil end end.compact end end |