diff --git a/app/assets/images/push-notifications/watching_first_post.png b/app/assets/images/push-notifications/watching_first_post.png new file mode 100644 index 00000000000..752e25c78d8 Binary files /dev/null and b/app/assets/images/push-notifications/watching_first_post.png differ diff --git a/app/services/push_notification_pusher.rb b/app/services/push_notification_pusher.rb index 85ff518a396..3874995eae9 100644 --- a/app/services/push_notification_pusher.rb +++ b/app/services/push_notification_pusher.rb @@ -5,7 +5,14 @@ class PushNotificationPusher CONNECTION_TIMEOUT_SECONDS = 5 def self.push(user, payload) + message = nil I18n.with_locale(user.effective_locale) do + notification_icon_name = Notification.types[payload[:notification_type]] + if !File.exist?(File.expand_path("../../app/assets/images/push-notifications/#{notification_icon_name}.png", __dir__)) + notification_icon_name = "discourse" + end + notification_icon = ActionController::Base.helpers.image_url("push-notifications/#{notification_icon_name}.png") + message = { title: payload[:translated_title] || I18n.t( "discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}", @@ -15,7 +22,7 @@ class PushNotificationPusher ), body: payload[:excerpt], badge: get_badge, - icon: ActionController::Base.helpers.image_url("push-notifications/#{Notification.types[payload[:notification_type]]}.png"), + icon: notification_icon, tag: payload[:tag] || "#{Discourse.current_hostname}-#{payload[:topic_id]}", base_url: Discourse.base_url, url: payload[:post_url], @@ -26,6 +33,8 @@ class PushNotificationPusher send_notification(user, subscription, message) end end + + message end def self.subscriptions(user) diff --git a/spec/services/push_notification_pusher_spec.rb b/spec/services/push_notification_pusher_spec.rb index ee542f41a08..e1b45b347bc 100644 --- a/spec/services/push_notification_pusher_spec.rb +++ b/spec/services/push_notification_pusher_spec.rb @@ -30,17 +30,27 @@ RSpec.describe PushNotificationPusher do PushSubscription.create!(user_id: user.id, data: data) end - def execute_push + def execute_push(notification_type: 1) PushNotificationPusher.push(user, { topic_title: 'Topic', username: 'system', excerpt: 'description', topic_id: 1, post_url: "https://example.com/t/1/2", - notification_type: 1 + notification_type: notification_type }) end + it "correctly guesses an image if missing" do + message = execute_push(notification_type: -1) + expect(message[:icon]).to eq("/assets/push-notifications/discourse.png") + end + + it "correctly finds image if exists" do + message = execute_push(notification_type: 1) + expect(message[:icon]).to eq("/assets/push-notifications/mentioned.png") + end + it "sends notification in user's locale" do SiteSetting.allow_user_locale = true user.update!(locale: 'pt_BR')